I have an angular.js controller (loginErrorCtrl) that is supposed to redirect to a view (/menu) when the data supplied from an input is equal to a specified string defined in the app (Data.serverToken).
function loginErrorCtrl($scope, Data, $location) {
$scope.data = Data;
$scope.validateToken = function(token) {
if (token != null) {
if (token.length == 4) {
if (token == Data.serverToken) {
$location.path('/menu');
} else {
//error
return "Invalid Token please try again";
}
}
}
};
}
The problem is that, when I type the correct token into the input box the $location.path('/menu') doesn't redirect until I hit the backspace. How can I get it to redirect upon successful validation of token?
Code listing on plunker : Angular JS routing
$scope.$apply()after setting the path. I didn't download your code, but it sounds like you might be calling validateToken() outside of Angular. A keypress will trigger a digest cycle.TypeError: Object #<Object> has no method 'apply' at Object.$scope.validateTokenapplymethod, but it does have an$applymethod -- you probably missed the$.$but here is the weird part. In chrome, i get the error:Error: $digest already in progress at Error (<anonymous>) at guntil the page crashes whereas in firefox, after a slight delay, it succesfully redirects. Your mention on$digesthas also sent me on a path on enlightenment i'm checking out this post Notes On AngularJS Scope Life-Cycle. If you could, please download the snippet it's just 36kb.. thanks!