[英]Angular ng-href doesn't work
我正在使用 Chrome 並設置了偵聽端口 8080 並提供所有列出的文件的節點服務器。 Angular app.js 假設顯示StuffView.html
的內容(簡單文本)。
索引.html:
<!doctype html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<title>angular</title>
<link rel="icon" href="">
<link rel='stylesheet' href="css/style.css">
<script src="libs/angular/angular.js"></script>
<script src="libs/angular-route/angular-route.js"></script>
<script src="app.js"></script>
<script src="js/controllers/StuffController.js"></script>
</head>
<body>
<div>
<a ng-href = "#/stuff"> show stuff </a>
<ng-view></ng-view>
</div>
</body>
</html>
應用程序.js:
angular
.module( 'app', [ 'ngRoute' ] )
.config( function( $routeProvider ) {
$routeProvider.when( '/stuff', {
templateUrl : "views/StuffView.html",
controller : "stuffController"
} );
} );
東西視圖.html:
<H1>Hello from Stuff View! {{hello}}</H1>
其中{{hello}}
來自 stuffController :
angular
.module( 'app' )
.controller( 'stuffController', stuffController );
function stuffController( $scope ) {
$scope.hello = 'Hello from stuffController! ';
}
當我單擊鏈接時,瀏覽器中的地址更改為http://localhost:8080/#!#%2Fstuff
並且沒有顯示任何內容。 控制台中沒有錯誤。 我錯過了什么?
我認為您以錯誤的方式使用了 ng-href,請參閱 ng-href 的角度文檔。
https://docs.angularjs.org/api/ng/directive/ngHref
您應該為 pathforstuff 創建一個名為 path 的變量,然后使用該變量,如下所示。
var pathforstuff="stuff";
<a ng-href="#/{{pathforstuff}}"/>Take Me Somewhere</a>
請參閱此 鏈接以了解ngRoute
模塊的使用。 不要在靜態情況下使用 'ng-href' 目錄,而是嘗試:
<a href = "#/stuff"> show stuff </a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.