Answer the question
In order to leave comments, you need to log in
[AngularJS] communication between service and controller data
During the development process, there were problems with the connectivity of links between the service and the controller.
Let me explain with an example:
// Есть сервис, в нем хранится id и другие данные.
app.service('testService', function ($http, serverURL, $timeout) {
var self = this;
self.data = {
id: null,
token: null,
....
};
self.initMe = function () {
return $http({
method: 'GET',
url: serverURL + '/initMe/' + '?token=' + self.data.token
});
};
return self;
});
meModule.controller('MeCtrl', function (...) {
$scope.me = testService.data; // устанавливаем связь между scope и service
$rootScope.$on('initMe', function (e, data) {
testService.initMe().success(function (data, status, headers, config) {
// data.result - object
// $scope.me = data.result; // не работает
// или
// meService.data = data.result; // не работает
meService.data = data.result; //
$scope.me = meService.data; // Всё ок - приходится восстанавливать связь между scope и service :(
});
});
}
Answer the question
In order to leave comments, you need to log in
Because testService.data
and $scope.me
are different references. If you want both of these references to point to the same object, then update both of them.
But why do you need two links? Just put in $scope
a link to your service and update the data only in it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question