I have encountered a problem in my Angular-ui-router where the use of resolve is causing my site to send out multiple get requests per second without loading the view. My stack consists of the full MEAN-stack.
The views are generated using inline templates, for example:
<script type="text/ng-template" id="/home.html">......</script>
These templates are then used within a <ui-view>
tag.
Please review this code snippet:
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 remove the Resolve section, the page loads fine.
EDIT: After further investigation, I discovered that the code I wrote was intended for angular version <= 1.4 and therefore I need to use .then instead of .success. However, when I switch to using .then, I lose the persistent data functionality that the factory is supposed to provide.
My new question is whether I should stick with AngularJS 1.4 or seek assistance in converting it to 1.6? Your input is greatly appreciated, thank you.