我们可以在控制器中明确调用Watch

时间:2014-11-28 07:49:14

标签: angularjs angularjs-directive angularjs-scope watch

我们可以在控制器中明确地拨打电话吗?

要求:

目前有两个角树结构复选框。一个是carriergorup,另一个是模式。我正在检索基于运营商组列表的模式值。每当载体组列表自动更新时,模式列表会更新为此用过的手表。 目前的功能就像。

每当更新运营商组列表时,只更新模式列表。假设运营商组列表没有变化,但我想明确地拨打电话。这可能吗?

$scope.$watch(
    'carrierGroups',
    function(carrierGroups) {
        alert("in carrier List");
        $scope.selectedCarrierList = [];
        $scope.isAllChecked = true;
        for (var i = 0; i < $scope.carrierGroups.length; i++) {
            /*if($scope.carrierGroups[i].checked){
                cntGroupChecked =  cntGroupChecked + 1;
            }*/
            for (var j = 0; j < $scope.carrierGroups[i].categories.length; j++) {
                if ($scope.carrierGroups[i].categories[j].checked) {
                    $scope.selectedCarrierList
                            .push($scope.carrierGroups[i].categories[j]);
                }else{
                    $scope.carrierGroups[i].checked = false;
                    $scope.isAllChecked = false;
                }
            }

        }
        alert("$scope.isModesWatch"+$scope.isModesWatch);

        if($scope.isModesWatch){
            $scope.modesList = DashboardsDataService.getModesData($scope.selectedCarrierList);

        }else{
            alert("$scope.filterModesList-->"+JSON.stringify($scope.filterModesList));
            $scope.modesList = $scope.filterModesList;
            $scope.isModesWatch = true;
        }
    }, 
true);

1 个答案:

答案 0 :(得分:0)

如下所示,你可以这样做..

var myCustomFun = function(carrierGroups) {
            alert("in carrier List");
            $scope.selectedCarrierList = [];
            $scope.isAllChecked = true;
            for (var i = 0; i < $scope.carrierGroups.length; i++) {
                /*if($scope.carrierGroups[i].checked){
                    cntGroupChecked =  cntGroupChecked + 1;
                }*/
                for (var j = 0; j < $scope.carrierGroups[i].categories.length; j++) {
                    if ($scope.carrierGroups[i].categories[j].checked) {
                        $scope.selectedCarrierList
                                .push($scope.carrierGroups[i].categories[j]);
                    }else{
                        $scope.carrierGroups[i].checked = false;
                        $scope.isAllChecked = false;
                    }
                }

            }
            alert("$scope.isModesWatch"+$scope.isModesWatch);

            if($scope.isModesWatch){
                $scope.modesList = DashboardsDataService.getModesData($scope.selectedCarrierList);

            }else{
                alert("$scope.filterModesList-->"+JSON.stringify($scope.filterModesList));
                $scope.modesList = $scope.filterModesList;
                $scope.isModesWatch = true;
            }
        }

$scope.$watch('carrierGroups',myCustomFun,true);

现在,您想要调用自定义监视功能,只需调用您的函数即可。

myCustomFun(param);