I am studying AngularJS and ionic structure using this tutorial:
http://www.htmlxprs.com/post/12/tutorial-on-using-parse-rest-api-and-ionic-framework-together
Everything works well until the moment when I create a new element in createTodo state and then call $ state.go ('todos') to return to the list of my elements, here is the code for creating Todo Controller:
.controller('TodoCreateController', ['$scope', 'Todo', '$state', function($scope, Todo, $state) { $scope.todo = {}; $scope.create = function() { Todo.create({content: $scope.todo.content}).success(function(data) { $state.go('todos', {}, {reload: true}); }); } }])
Here is the code for the item list controller:
.controller('TodoListController', ['$scope', 'Todo', '$state', function($scope, Todo) { Todo.getAll().success(function(data) { $scope.items = data.results; }); $scope.deleteItem = function(item) { Todo.delete(item.objectId); $scope.items.splice($scope.items.indexOf(item), 1); }; }])
The states are configured here.
.config(function($stateProvider) { $stateProvider.state('todos', { url: '/todos', controller: 'TodoListController', templateUrl: 'views/todos.html' }).state('createTodo', { url: '/todo/new', controller: 'TodoCreateController', templateUrl: 'views/create-todo.html' }); })
When the application starts, the TodoListController method is called thanks to the last line added and the end of the .run method in the main app.js (or at least this is my understanding):
.run(function($ionicPlatform, $state) { $ionicPlatform.ready(function() { if(window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if(window.StatusBar) { StatusBar.styleDefault(); } }); $state.go('todos'); })
My problem is that as soon as a new element is created and I call $ state.go ('todos') from TodoCreateController, it returns me to the list of elements, but the new element is not there, and the method from TodoListController is never called, therefore leaving the list obsolete.
How to update the list in the "todos" state after creating a new item?