I'm trying to learn angular with this tutorial https://thinkster.io/tutorials/mean-stack/wiring-everything-up and I've hit a snag where opening the application runs into an infinite loop of calling the /posts url. I've checked my routes with postman and they are working as intended so I'm not sure why this isn't resolving correctly. Below is the 'posts' service, 'mainCtrl' controller, and the config. Could someone take a look and tell me if they see my error or if I need provide more of the code to help. Thank you.
Service
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;
}])
Controller
app.controller('MainCtrl', ['$scope', 'posts', function($scope, posts){
$scope.test = 'Hello world!';
$scope.posts = posts.posts;
$scope.addPost = function(){
if(!$scope.title || $scope.title === '') { return; }
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: 0,
comments: [
{author: 'Joe', body: 'Cool post!', upvotes: 0},
{author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
]
});
$scope.title='';
$scope.link='';
}
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
}]);
Config
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();
}]
}
})
.state('posts', {
url:'/posts/{id}',
templateUrl:'/posts.html',
controller:'PostsCtrl'
})
$urlRouterProvider.otherwise('home');
}])