更新到AngularJS 1.6时,Routing Demo停止工作 [英] Routing Demo stops working when updated to AngularJS 1.6

查看:92
本文介绍了更新到AngularJS 1.6时,Routing Demo停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从此答案更新代码片段以使用AngularJS 1.6时,它停止工作。

When I updated the code snippet from this answer to use AngularJS 1.6, it stopped working.

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

The Login and Register links no longer change the view.

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>

推荐答案

Angular 1.6中的路由从 #/州 #!/州

Routes in Angular 1.6 changed from #/state to #!/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>

这篇关于更新到AngularJS 1.6时,Routing Demo停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆