路由演示在更新到AngularJS 1.6时停止工作



当我更新这个答案的代码片段以使用AngularJS 1.6时,它停止了工作。

登录Register

DEMO

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中的路由从#/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>

相关内容

  • 没有找到相关文章

最新更新