简体   繁体   English

路由在Angular.js中不起作用

[英]Routing not working in Angular.js

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 ). 我有一个angular.js控制器( loginErrorCtrl ),当输入提供的数据等于应用程序中定义的指定字符串( Data.serverToken )时,该控制器应重定向到视图( /menu )。

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. 问题是,当我在输入框中输入正确的令牌时, $location.path('/menu')不会重定向,直到我按下退格键。 How can I get it to redirect upon successful validation of token? 成功验证令牌后,如何获得重定向?

Code listing on plunker : Angular JS routing 在plunker上的代码清单: Angular JS路由

The correct answer was to put the $scope.$apply() as Mark suggested in the comments like so: 正确的答案是将$ scope。$ apply()放入Mark的注释中,如下所示:

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');
      $scope.$apply()
      } else {
      //error
      return "Invalid Token please try again";
    }
  }
}
};
}

corrected code 更正的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM