【发布时间】:2014-07-11 17:41:22
【问题描述】:
我尝试使用 1 个包含模板的 index.html 创建一个简单的 SPA。
我遇到了 ng-href 指令的问题:
<a ng-href="#/myPage">myPage</a>
在 index.html 中工作,但不在我的模板中,该链接不可点击。但 href 仍然有效。
<a href="#/myPage">myPage</a>
我的应用:
index.html:
...
<body ng-app="myApp">
<a ng-href="#/myPage" target="_self">link</a> <!-- work ! -->
<div class="container" ng-view=""></div>
</body>
...
app.js:
'use strict';
angular.module('myApp',
[ 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute' ]).config(
function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'views/main.tpl.html',
controller : 'MainCtrl'
})
.when('/myPage', {
templateUrl : 'views/page.tpl.html',
controller : 'MainCtrl'
})
.otherwise({
redirectTo : '/'
});
});
controller.js
'use strict';
myApp.controller('MainCtrl', function() {
this.myColor = 'blue';
});
page.tpl.html
<div>
<a ng-href="#/myPage" target="_self">link</a> <!-- Dont work -->
<a ng-href="#/myPage" target="_self">link</a> <!-- Dont work -->
<a ng-href="#/myPage{{}}">link</a> <!-- Dont work -->
<a ng-href="#/{{ 'myPage' }}">link</a> <!-- Dont work -->
<a href="#/myPage" target="_self">link</a> <!-- Work ! -->
</div>
我不明白 ng-href 的问题以及为什么结果与 href 不同。 我使用角度 1.2
【问题讨论】:
标签: javascript html angularjs templates