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屋!