gpt4 book ai didi

javascript - AngularJS : Routing

转载 作者:行者123 更新时间:2023-11-28 18:03:00 26 4
gpt4 key购买 nike

所以我正在观看一个 Angular 教程,那个人正在解释“ngRoute”以及如何使用它,使用这个例子:

var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'pages/home.html',
controller: 'mainController'


})
.when('/second', {
templateUrl: 'pages/second.html',
controller: 'secondController'

})

});


myApp.controller('mainController', ['$scope', '$route', function ($scope, $route) {




}])
.controller('secondController', ['$scope', '$route', function ($scope, $route) {




}]);
<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
</head>

<body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Angular</a>
</div>
<div id="navbar" class="navbar-collapse collapse">

<ul class="nav navbar-nav navbar-right">
<li><a href="#">Home</a></li>
<li><a href="#/second">Second</a></li>
</ul>
</div>
</div>
</nav>

<div class="container">

<div ng-view class="jumbotron"></div>

</div> <!-- /container -->




<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-route.js"></script>
<script src="app.js"></script>
</body>



</html>

这对他有用,但对我来说,只有主页链接有效,而第二个链接不起作用,直到我更改了 <a href="#/second">Second</a><a href="#!/second">Second</a>它有效,但我不明白为什么?

最佳答案

您可能使用的是 1.6 版本,对吗? 。在这个 1.6 Angular 团队中决定将默认的 $location 哈希前缀更改为 '!' 。您可以阅读

https://docs.angularjs.org/guide/migration#migrating-from-1-5-to-1-6

关于javascript - AngularJS : Routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43204885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com