Answer the question
In order to leave comments, you need to log in
How to properly update the $scope of an Angular controller?
Good afternoon friends!
Let's say I have a function in my controller that is responsible for passing some array received from the server to $scope
function MyCtrl($scope, req, calc){
$scope.update = function() {
req.post('c=users&a=getAll', {}, function (res) {
res = calc.calc(res[i]);
$scope.items = res;
}
});
}
$scope.update();
}
function Add($scope, req){
$scope.save = function(){
req.post('c=users&a=add', $scope.mydata, function(res){
//в этом callback необходимо что то вроде MyCtrl.update();
});
}
}
Answer the question
In order to leave comments, you need to log in
You need to write a service that encapsulates all external interactions.
User
.getAll
.add (после успешного выполнения запроса вызывает .onUserAdded)
.onUserAdded
function MyCtrl($scope, req, calc, User){
$scope.update = function() {
User.getAll(function (res) {
res = calc.calc(res[i]);
$scope.items = res;
}
});
}
User.onUserAdded($scope.update);
$scope.update();
}
function Add($scope, req, User){
$scope.save = function(){
User.add($scope.mydata);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question