I have a question how to get information on how to get data stored in service arrays to the controller, since the deployment of promises was removed in 1.2.
Example:
- Route One contains a list of items
- Route two contains a form for adding a new item
- Once the item has been saved from the route tug, the user is redirected back to the first route.
When the first route is initially loaded, the service will make a request to the server for the elements, store the elements in an array in the service, so that each request for the route alone after that simply returns an array. When an item was saved, that item was moved to an array in the service.
If I expected a promise in the first controller on boot, there is no problem because the response was sent back, but each routing request alone would return an error after that, because I was returning an array.
Any ideas on how to accomplish something like this in 1.2?
app.factory('Items',function($http) {
var items = [];
return {
list: function() {
if (items.length == 0) {
return $http.get('/?items').then(function(response) {
items = response.data.items;
return response.data.items;
});
}
return items;
},
save: function(item) {
return $http.post('/',{item:item}).then(function(response) {
items.push(item);
return response;
});
}
}
});
app.controller('RouteOne',function($scope,Items) {
Items.list().then(function(response) {
$scope.items = response;
});
});
app.controller('RouteTwo',function($scope,Items) {
$scope.new_item = {};
$scope.addItem = function() {
Items.save($scope.new_item).then(function(response) {
$location.path('/');
});
};
});
source
share