I’ve been struggling to find a solution to my problem. My problem is I fetch json data from http request inside a factory, now when I change my data from the edit page the other page does not update also. I want to let the other page change data as well when the data inside the factory/services has been changed also… Thanks a lot…
Hi @anicacute09. You could use the state params to pass data from one state to another.
Also you could try and have a parent scope store the data then access the data object as a reference from the child scope. i.e
var app = angular.module('myApp', ['ionic', 'ngResource']);
...
app.controller('GlobalScope', ['$scope', '$myFactory', function($scope, $myFactory) {
$scope.appData = {
messages: $myFactory().data
};
}]);
app.controller('ChildScope', ['$scope', '$myFactory', function($scope, $myFactory) {
$scope.setNewValue = function(newVal) {
$scope.appData.messages = newVal; // This will update the parent data too
$myFactory().data = newVal; // Well maybe, not sure, you'd want this with something as powerful as ngResource
};
}]);
// Consider using 'ngResource' wih your factory so you can have something like
app.controller('GlobalScope', ['$scope', '$myFactory', function($scope, $myFactory) {
$scope.appData = {
messages: new $myFactory({data:'Initial Value'})
};
}]);
app.controller('ChildScope', ['$scope', '$myFactory', function($scope, $myFactory) {
$scope.setNewValue = function(newVal) {
$scope.appData.messages.data = newVal; // Updates parent value because this is referential
$scope.appData.messages.$save(); // Sends data to server
};
}]);
Or use $scope.$watch() to detect changes to a particular value within your controller
I already fix my issue I just use rootScope.emit to my factory and listen to it for checking/watching if data has change… So as the factory data change the other controllers data has change to dynamically. Btw thanks for the help…