In my controller I have a method that sends a POST request to my REST API. On success/error I want to display an success/error message using ng-show. The problem is that my scope variable is changed but my view is not being updated.
I have tried using $scope.$apply() but that throws $digest error.
Below is my current code. Any ideas?
function AdminController($scope, $http, $timeout) {
$scope.addUserError = false;
$scope.addUserSuccess = false;
$scope.createUser = function (newuser) {
$http.post("rest/user", JSON.stringify(newuser)).success(function () {
console.log("User added");
$timeout(function () {
$scope.addUserSuccess = true;
$scope.addUserError = false;
});
}).error(function () {
$timeout(function () {
$scope.addUserSuccess = false;
$scope.addUserError = true;
console.log($scope.addUserError); //true
});
})
}
}
timeout-$httpwill trip a$digestcycle - are there any errors in your call?$scope.addUserSuccessyou'd have$scope.model.addUserSuccess. Sometimes this is all you need to fix something like this.error {{addUserError}}<br> success {{addUserSuccess}}. It is being shown correctly.