【发布时间】:2015-12-16 20:00:51
【问题描述】:
我遇到的问题是,一旦将 templateUrl 添加到 ui-router 子状态,应用程序将不再执行到该状态的路由。当它只是一个模板时它工作得很好。
app.js:
app.config(['$stateProvider', '$locationProvider', '$urlMatcherFactoryProvider', '$urlRouterProvider',
function ($stateProvider, $locationProvider, $urlMatcherFactoryProvider, $urlRouterProvider) {
$urlMatcherFactoryProvider.caseInsensitive(true);
$urlMatcherFactoryProvider.strictMode(false);
$urlRouterProvider.otherwise('/page-not-found');
$stateProvider
.state('dashboard', {
url: '/',
views: {
'header': {
template: 'header'
},
'nav': {
template: 'nav'
},
main: {
template: 'You are on the homepage'
}
}
});
$locationProvider.html5Mode(true);
}]);
app.run(['$rootScope', 'userService', '$state', function ($rootScope, user, $state) {
$rootScope.$on("$stateChangeError", console.log.bind(console));
if (!user.exists) {
$state.go('user.reg');
}
}]);
User.states.js:
.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('user', {
url: '/users',
abstract: true,
views: {
'header': {},
'nav': {},
'main': {
template: '<ui-view/>'
}
}
})
.state('user.reg', {
url: '/register',
//template: 'This will show fine',
templateUrl: '/app/Users/User.login.html' // this will break
});
}]);
更新
如果我在初始页面中添加 ui-sref="user.reg",我可以使用 templateUrl 和 template 导航到状态/页面。因此,当我尝试使用 state.go('user.reg');
这意味着解决方法是使用$location 提供程序来更改路径。具有相同的效果,但似乎相当错误
【问题讨论】:
-
当它中断时,您会看到控制台错误吗?模板url路径是404吗?
-
@Starscream1984 完全没有错误,文件存在并且没有 404。它甚至没有路由到 user.register 状态,只是停留在索引状态
-
我在您的示例和我的工作示例之间看到的唯一区别是,在我的代码中,url 字符串中没有前导斜杠...
-
@Starscream1984 删除后我得到了同样的效果。你使用的是什么版本的 angular/ui-router?
-
角度 - v1.4.7 ui-router - v0.2.15
标签: javascript angularjs angular-ui-router