Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am trying to call a $http.get method to load contents of page (which is a list of email addresses).

When my page loads, it continuously calling the http.get method, while i want to call it just once to load the email address and then store it in scope variable.

My view code is

<h2>Members</h2>

<!--<div>{{getTeamMembers()}}</div>-->
<div ng-repeat="member in teamParticipants">
   <span> </span> <span>{{member}}</span>
</div>

My controller class is

Controllers.controller('FixedTeamController', function ($scope,$http,$location) {

$scope.addTeam = function() {
    $http.post('team', $scope.team).
    success(function(fixedTeam, status, headers, config) {
          //succcess
    }).
    error(function(fixedTeam, status, headers, config){
        console.debug("error");
    });
};

$scope.getTeamMembers = function(){

var teamUId = "12541254";

$http({method: 'GET', url: 'team/'+teamUId}).
success(function(data, status, headers, config) {
      var members = [];
      $(data.listOfMembers).each(function(id,partcipantEmail){
          $http({method: 'GET', url: 'users?email='+partcipantEmail}).
          success(function(participant, status, headers, config) {
              members.push("new member");
          })
      });
      $scope.teamParticipants = members;
}).
error(function(data, status, headers, config) {
      console.debug("error");
});

};

});

Code works fine, but it is calling the getTeamMembers() function continuously, so my question is that what should i do to call the function just once.

Edit :: Added other method "addTeam" which is use to add new team members (it uses different view). By calling the function from inside controller partially solved the problem, but when now as i load the addTeam view, $scope.getFixedTeam() function also called, i don't want to call this function. How can i do that ? Regards,

share|improve this question
 
Comment out <div>{{getTeamMembers()}}</div>. and see what happens –  dcodesmith Aug 1 at 9:23
add comment

2 Answers

You put the function call in a binding, which means it will be executed on every apply-digest[1]. Instead, put it the call to getTeamMembers in your controller, and use the teamParticipants property in your views.

[1]: {{}} is a two-way binding in Angular, and if you're binding to a function, it has to evaluate the result every time to find out if it's something new. That means that every time AngularJS checks for dirty states, it will call all the functions in all the bindings. This can add up to a very expensive cycle, so should only be done if necessary!

share|improve this answer
 
Hi, thanks a lot for your time, adding function works, i have added the complete controller code, so when i navigate to add new team view, it uses the same controller and it calls the function automatically. I want to avoid calling this function on add new team view. How can i handle that. thanks for your time. –  Shahzeb Aug 1 at 9:32
 
It sounds like it should be a different controller if it's a different view, but if it absolutely has to be the same controller there needs to be some sort of if statement to only load the team members when they are needed. In your updated code, I still see the function call in the view, and not in the controller, by the way! –  stevuu Aug 1 at 9:38
 
i updated the code now. –  Shahzeb Aug 1 at 9:41
add comment

Dont need of calling the getTeamMembers() in the html

you can call it after the function definition.(below the function)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.