$ rootScope。$ broadcast在页面重新加载时没有更新?

时间:2017-03-14 06:40:03

标签: angularjs scope broadcast rootscope

第一控制器:

$rootScope.$broadcast("CallParentMethod", {});

第二名管制员:

$rootScope.$on("CallParentMethod", function() {
    $scope.getUserDetails();
})
$scope.getUserDetails = function() {
    HttpService.get("/customer/" + nationalId).then(function(resp) {
        if (resp.status == 'success') {
            console.log(resp.result)
            $rootScope.county_name = angular.copy(resp.result.county)
            $rootScope.campaign_name = angular.copy(resp.result.campaign)
            console.log($rootScope.campaign_name)
        }
    });
};

1 个答案:

答案 0 :(得分:0)

$scopes混合

此事件被解雇

$rootScope.$broadcast("CallParentMethod", {});

因此,您应该在$ $范围内收听此活动,但您正在$rootScope

中收听该活动
$rootScope.$on("CallParentMethod", function() {
    $scope.getUserDetails();
})

您可能希望在定义getUserDetails()的同一范围内听取它

$scope.$on("CallParentMethod", function() {
    $scope.getUserDetails();
})

另一个提示:如果您不需要传递任何参数,则可以在{}

时忽略空对象$broadcasting
$rootScope.$broadcast("CallParentMethod")