0

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
'<span ng-if="fields.type===\'direct\'" class="text-default "> '+ ' {{ ngDirectiveFunc( {valueId:row[fields.colvalue], directName:fields.direct} ) }}</span>'+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.