Man*_*noz 5 angularjs angularjs-directive angular-broadcast
我试图让 $rootScope.$broadcast 刷新我的视图。服务是——
var app = angular.module("productsApp", [])
.service("serviceProvider", function ($http) {
this.getDatas = function getDatas(data) {
return $http({
method: 'POST', url: serviceUrl + '/GetProductsByCategoryOrName', headers: {
'Authorization': apiKey
},
data: data
})
}
return this
}).factory("sharedService", function ($rootScope) {
var mySharedService = {};
mySharedService.values = [];
mySharedService.passData = function (newData) {
mySharedService.values = newData;
$rootScope.$broadcast('dataPassed', newData);
}
return mySharedService;
});
Run Code Online (Sandbox Code Playgroud)
通过控制器调用-
function searchProductsController($scope, $window, serviceProvider, sharedService) {
$scope.submit = function () {
var data = { "query": $scope.searchText, "categoryId": "976759", "pageIndex": 0, "sortDirection": "asc" };
serviceProvider.getDatas(data).then(function (response) {
var result = response;
sharedService.passData(result.data.data);
});
}
};
Run Code Online (Sandbox Code Playgroud)
在这个控制器中sharedService.passData,它将新数组传递给服务方法。然后它试图用线路广播它的变化 - $rootScope.$broadcast('dataPassed', newData)
我不知道为什么它不广播要查看的更改。有没有其他方法可以广播更改?
注意 - 我之前的问题如何在控制器之间传输数据对我没有任何帮助。
编辑
到目前为止,我已经改变了听众——
mySharedService.passData = function (newData) {
$rootScope.$broadcast('dataPassed', newData)
}
$rootScope.$on('dataPassed', function (newData) {
mySharedService.values = newData;
})
Run Code Online (Sandbox Code Playgroud)
但仍然无法刷新视图。
当您使用 $broadcast 或 $emit 时。您应该有 $scope.$on 来监听该事件。
$scope.$on('dataPassed', function(){ //code go here });
Run Code Online (Sandbox Code Playgroud)
编辑:更新问题要求的工作代码
var app = angular.module("productsApp", [])
.service("serviceProvider", function ($http) {
this.getDatas = function getDatas(data) {
return $http({
method: 'POST', url: serviceUrl + '/GetProductsByCategoryOrName', headers: {
'Authorization': apiKey
},
data: data
});
}
return this
}).factory("sharedService", ['$rootScope', function ($rootScope) {
var mySharedService = {
values: [],
setValues: function(data){
if(data){
mySharedService.values.length = 0;
data.forEach(function(item){
mySharedService.values.push(item);
});
}
}
};
return mySharedService;
}]);
function GetController($scope, serviceProvider, sharedService, $rootScope) {
var shareData = sharedService;
$scope.products = sharedService.values;
$scope.shareData = sharedService;
var data = { "query": "grocery", "categoryId": "976759", "pageIndex": 0, "sortDirection": "asc" };
serviceProvider.getDatas(data).then(function (response) {
sharedService.setValues(response.data.data);
});
}
function searchProductsController($scope, $window, serviceProvider, sharedService, $rootScope) {
$scope.submit = function () {
var data = { "query": $scope.searchText, "categoryId": "976759", "pageIndex": 0, "sortDirection": "asc" };
serviceProvider.getDatas(data).then(function (response) {
sharedService.setValues(response.data.data);
});
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12380 次 |
| 最近记录: |