Reputation: 1060
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
Upvotes: 0
Views: 463
Reputation: 8465
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
Upvotes: 1