$rootScope.$broadcast 不工作 [英] $rootScope.$broadcast not working

查看:21
本文介绍了$rootScope.$broadcast 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 $rootScope.$broadcast 来刷新我的视图.服务是-

I am trying to get $rootScope.$broadcast to refresh my view. The service is-

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;
    });

通过控制器调用-

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);
        });
    }
};

在这个控制器中 sharedService.passData 它将新数组传递给服务方法.然后它尝试使用以下行广播它的更改 - $rootScope.$broadcast('dataPassed', newData)

here in this controller sharedService.passData which passes new array to service method. Then it is trying to broadcast it for the changes with the line- $rootScope.$broadcast('dataPassed', newData)

我不知道为什么它不广播要查看的更改.还有其他方法可以广播更改吗?

I don't know why it is not broadcasting the changes to view. Is there any other way to broadcast the changes?

注意-我之前的问题如何在控制器之间传输数据 无法给我任何帮助.

Note- My earlier question How to transfer data between controllers couldn't get me any help.

编辑

到目前为止,我已经改变了听众 -

So far I've changed in the listeners-

mySharedService.passData = function (newData) {
    $rootScope.$broadcast('dataPassed', newData)
}
$rootScope.$on('dataPassed', function (newData) {
    mySharedService.values = newData;
})

但仍然无法刷新视图.

推荐答案

当您使用 $broadcast 或 $emit 时.你应该有 $scope.$on 来监听那个事件.

When you use $broadcast or $emit. You should have $scope.$on to listen to that event.

$scope.$on('dataPassed', function(){ //code go here });

更新问题要求的工作代码

Update working code for question requirement

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);
        });
    }
};

这篇关于$rootScope.$broadcast 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆