Angular ui-router templateProvider从未调用 [英] Angular ui-router templateProvider never called
问题描述
我需要将路由参数传递给使用模板响应的服务器,因此我尝试每隔几篇文章/其他堆栈溢出文档使用templateProvider
.
I need to pass a route parameter to the server responding with a template, so I'm trying to use a templateProvider
per several articles/other stack overflow docs.
我没有收到任何JavaScript错误,但是以下templateProvider
方法从未执行过.如果未注释templateUrl
属性,则此路由可以正常工作.
I'm not getting any javascript errors, but the following templateProvider
method is never even executed. When the templateUrl
property is not commented out, this route works fine.
$stateProvider
.state('org.contacts.add', {
url: '/add',
views: {
'org@': {
// templateUrl: '/templates/issues/add',
controller: 'ContactsAddController',
templateProvider: function($route, $templateCache, $http) {
var url = '/templates/' + $route.current.params.org + '/contacts/add';
$http.get(url, {cache: $templateCache}).then(function(html){
return html;
});
}]
}
}
})
经过一些实验,似乎$route
引起了麻烦.删除并使用$stateParams
至少会触发此代码/控制器.
After some experimentation, it seems the $route
was causing trouble. Taking that out and using $stateParams
at least fires this code/controller.
但是,虽然我看到ajax调用触发和正确的html响应,但它从未加载到视图中.
However, while I see the ajax call firing and the proper html response, it's never loaded to the view.
templateProvider: function ($stateParams, $http, $templateCache) {
var url = '/templates/contacts/add';
$http.get(url, {cache: $templateCache}).then(function(html){
return html;
});
}
推荐答案
我猜测您需要返回$promise
才能正常工作.查看 UI-Router Wiki 上使用的示例:
I'm guessing you need to return a $promise
for this to work. Check out the example used on the UI-Router wiki:
$stateProvider.state('contacts', {
templateProvider: function ($timeout, $stateParams) {
return $timeout(function () {
return '<h1>' + $stateParams.contactId + '</h1>'
}, 100);
}
})
请注意以return $timeout(...
开头的行.您是否尝试过返回通过执行$http.get()
创建的$promise
?
Notice the line that begins with return $timeout(...
. Have you tried returning the $promise
that is created by doing $http.get()
?
这篇关于Angular ui-router templateProvider从未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!