[英]Angular-UI-router resolve not working
I got this problem where when i use resolve in Angular-ui-router causes my site to do hundreds of get requests per second but not load the view. 我遇到了这个问题,当我在Angular-ui-router中使用resolve时,我的网站每秒会执行数百次获取请求但不加载视图。 I am using the full MEAN-stack.
我正在使用完整的MEAN堆栈。
The views are created with inline templates eg: 使用内联模板创建视图,例如:
<script type="text/ng-template" id="/home.html">......</script>
And then used in a <ui-view>
tag. 然后在
<ui-view>
标签中使用。
Please look at this code: 请看这段代码:
app.factory('posts', ['$http', function($http) {
var o = {
posts: []
};
o.getAll = function() {
return $http.get('/posts').success(function(data){
angular.copy(data, o.posts);
});
};
return o;
}]);
app.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl',
resolve : {
postPromise : ['posts',
function(posts) {
return posts.getAll();
}]
}
})
If i dont use the Resolve, the page is fine, it loads. 如果我不使用Resolve,页面很好,它会加载。
EDIT: I have boiled it down to that the code i wrote was for angular version <= 1.4 and therefore i need to use .then instead of .success, but when i use .then i dont get the persistent data that the factory is supposed to do. 编辑:我已经把它归结为我写的代码是角度版本<= 1.4因此我需要使用.then而不是.success,但是当我使用。然后我不会得到工厂的持久数据去做。
So new question, should i just stay on angularJs 1.4 or try/get help to convert this to 1.6? 所以新的问题,我应该继续使用angularJs 1.4或尝试/获得帮助将其转换为1.6? Appreciate all the answers, thank you.
感谢所有答案,谢谢。
Instead of directly returning $http
promise
object, take promise
object into a variable and return it. 而不是直接返回
$http
promise
对象,将promise
对象转换为变量并返回它。 Please do as fallows, it worked for me. 请做休闲,它对我有用。
var promise = $http.get('posts');
promise.then(function(response) {
},
function(data) {
});
return promise;
Please note success
and error
call backs are deprecated in angular 1.6
, use then
. 请注意
success
和error
回调在angular 1.6
中已弃用, then
使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.