【发布时间】:2016-08-25 06:31:02
【问题描述】:
我正在使用 ui-router 但它不起作用。这是我的代码:
在我的 index.html 中
<li> <a ui-sref="day2">Day 2</a> </li>
<li><a ui-sref="day3">Day 3</a></li>
<li><a ui-sref="day4">Day 4</a></li>
我的 main.js
var myModule = angular.module('myApp', ['ngMessages', 'ui.router']);
myModule.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/day1");
$stateProvider
.state('day1', {
url: '/day1',
templateUrl: 'index.html'
})
.state('day1.example', {
url: '/theview',
template: '<h3>I am a routed view</h3>'
})
.state('day2', {
url: '/day2',
views: {
// the main template will be placed here (relatively named)
'': {
templateUrl: 'day2.html'
}
}
})
.state('day3', {
url: '/day3',
views: {
// the main template will be placed here (relatively named)
'': {
templateUrl: 'day3.html'
},
// the child views will be defined here (absolutely named)
'directives@day3': {
templateUrl: 'directives.html'
},
// for column two, we'll define a separate controller
'services@day3': {
templateUrl: 'service.html'
}
}
})
.state('day4', {
url: '/day4',
views: {
'': {
templateUrl: 'day3.html'
}
}
});
});
当我在浏览器中单击第 2 天、第 3 天和第 4 天的链接时,它不起作用。甚至 day1.example 也不起作用,但我在 index.html 中这样称呼它
<div>
<a ui-sref=".example" class="save button">Example</a>
</div>
<div ui-view> </div>
</div>
我不确定问题是什么。我已经下载了 ui-router 压缩文件,所以这不是问题。它实际上是在浏览器file:///Users/Projects/AngularJs/index.html#/day1中检测day1路由
我正在关注scotch 教程。
我的问题在于templateUrl: 'day2.html,当我将其更改为template: '<h1> Check if it Works </h1> 时,第 2 天的链接运行良好。
【问题讨论】:
-
不要将状态模板设置为使用
index.html。
标签: javascript angularjs routing angular-ui-router