更新时间:2023-11-17 11:30:58
clientDataService.clientDataObject是不是你的控制器范围的一部分,所以你不能观看该对象上的变化。
您需要注入$ rootScope到您的服务,然后将更改广播到控制器的作用域。
clientDataService.clientDataObject is not part of your controller's scope, so you can't watch for changes on that object. You need to inject the $rootScope into your service then broadcast the changes to the controllers scopes.
app.factory('clientDataService', function ($rootScope, $http) {
var clientDataObject = {};
var cdsService = {
fetch: function (cid) {
var promise = $http.get('/clients/stats/' + cid + '/').then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response);
// The return value gets picked up by the then in the controller.
clientDataObject = {'data': response.data, 'currentClientID': cid};
$rootScope.$broadcast('UPDATE_CLIENT_DATA', clientDataObject);
return clientDataObject;
});
// Return the promise to the controller
return promise;
}
};
return cdsService;
});
然后在控制器,你可以听使用的变化:
Then in the controller you can listen for the change using:
$scope.$on('UPDATE_CLIENT_DATA', function ( event, clientDataObject ) { });