更新到AngularJS 1.6时,Routing Demo停止工作

时间:2018-08-04 13:13:12

标签: angularjs angularjs-ng-route angularjs-1.6

当我将this answer中的代码段更新为使用AngularJS 1.6时,它停止工作。

登录 Register 链接不再更改视图。

演示

var app = angular.module("myApp", ["ngRoute"])
app.config(function($routeProvider) {
  $routeProvider.when('/', {
      template: `<h1>Login</h1>`,
      controller: 'loginCtrl'
    })
    .when('/register', {
      template: `<h1>Register</h1>`,
      controller: 'RegisterCtrl'
    })
    .otherwise({
      redirectTo: '/'
    });

});
app.controller('loginCtrl', function($scope) {
  $scope.name = "Login";

});
app.controller('RegisterCtrl', function($scope) {
  $scope.name = "Register";

})
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <meta charset="utf-8" />
  <title>AngularJS User Registration and Login Example  </title>
</head>

<body>
  <a href="#/login">Login</a>
  <a href="#/register">Register</a>
  <div class="mainContainer" ng-view></div>
  <script src="//unpkg.com/angular@1.6/angular.js"></script>
  <script src="//unpkg.com/angular-route@1.6/angular-route.js"></script>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

Angular 1.6中的路由从 #/state 更改为 #!/state

您应该将参考更改为:

 <a href="#!/login">Login</a>
 <a href="#!/register">Register</a>

演示

var app = angular.module("myApp", ["ngRoute"])
app.config(function($routeProvider) {
  $routeProvider.when('/', {
      template: `<h1>Login</h1>`,
      controller: 'loginCtrl'
    })
    .when('/register', {
      template: `<h1>Register</h1>`,
      controller: 'RegisterCtrl'
    })
    .otherwise({
      redirectTo: '/'
    });

});
app.controller('loginCtrl', function($scope) {
  $scope.name = "Login";

});
app.controller('RegisterCtrl', function($scope) {
  $scope.name = "Register";

})
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <meta charset="utf-8" />
  <title>AngularJS User Registration and Login Example  </title>
</head>

<body>
  <a href="#!/login">Login</a>
  <a href="#!/register">Register</a>
  <div class="mainContainer" ng-view></div>
  <script src="//unpkg.com/angular@1.6/angular.js"></script>
  <script src="//unpkg.com/angular-route@1.6/angular-route.js"></script>
</body>

</html>