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

How can I call the function once. When I do so, calling it loops. HTML: calling function from controller. find value of index.

    <body ng-controller="MainCtrl">
       <div ng-repeat="id in arr">
    {{sendPost(id)}}
      </div>
    <br>
        {{namestr}}
    </body>

JS: function request server for value

    app.controller('MainCtrl', function($scope, $http) {
    $scope.arr=[];
    $scope.arr.push(1);
    $scope.arr.push(2);
    $scope.arr.push(3);   
    $scope.namestr="";
    $scope.newName = "";
    $scope.sendPost = function(names) {
        $scope.namestr=$scope.namestr+' '+names;
        var data = $.param({
            json: JSON.stringify({
                name: names
            })
        });
        $http.post("/echo/json/", data).success(function(data, status) {
          //select from database
           if (data==1){
             return '1111'
           } else {
             return '2222'
           }
        })
    }                            
    });

http://plnkr.co/edit/Zhao5JuEeuG1KGXNhLS0?p=preview

share|improve this question

1 Answer 1

This is bad idea in general

{{sendPost('Василий')}}

sendPost method and together with it http.post will be called with every single $digest process, and $digest is triggered by model change so this

$scope.hello = data;

will cause another $digest and another http request, explain more what you want to achieve and I'll extend your plunker

share|improve this answer
    
I have a directive of the table. Directive receives the data and renders the table. At the time of rendering, depending on the type of field, the value field requests from the directory. Directory in the database. The directive calls a function on the link from the main controller and receives the value of the field. –  Groxot Oct 20 '14 at 9:56
    
'<span ng-if="fields.type===\'direct\'" class="text-default "> '+ ' {{ ngDirectiveFunc( {valueId:row[fields.colvalue], directName:fields.direct} ) }}</span>'+ –  Groxot Oct 20 '14 at 9:57

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.