如何在AngularJS中向其他控制器广播功能?

时间:2015-04-21 15:44:14

标签: javascript angularjs kendo-treeview

我有三个屏幕,每个屏幕具有相同的功能,用于位置树视图下拉我在下面实现功能,现在我想在任何需要位置树视图下拉的应用程序中使用它。让我说我有另一个控制器Ctrl2.js我怎么可以使用updateGeolocationList来自Ctrl1.js.Any的()函数将不胜感激。

Ctrl1.js

$scope.updateGeoLoctionList = function(geoLocation){
        var pos = $.inArray(geoLocation.text, selectedGeoLocations);

        if(pos < 0){
            selectedGeoLocations.push(geoLocation.text);
            selectedGeoLocationIds.push(geoLocation.id);
            //if Global is checked disable all parent and children
            if (geoLocation.id === 5657){
              $.each(geoLocation.parent(),function(index,location) {
                if (location.id !== geoLocation.id) {
                  $.each(location._childrenOptions.data.items,function(index,child){
                    var disableChildId = 'disabled' + child.id;
                    var model = $parse(disableChildId);
                      model.assign($scope, true);
                  })
                  var disableItemId = 'disabled' + location.id;
                  // Get the model
                  var model = $parse(disableItemId);
                  // Assigns a value to it
                  model.assign($scope, true);

                }
              });
            }
}

Config.js

geoLocationTreeConfig : {
        template: '{{dataItem.text}}',
        checkboxes: {
            checkChildren: false,
           template: '<input ng-disabled=\'disabled#: item.id #\' type=\'checkbox\' ng-click=\'updateGeoLoctionList(dataItem)\' value=\'true\' />'
        }
    },controlInPrcsGeoLocationTreeConfig : {
        template: '{{dataItem.text}}',
        checkboxes: {
            checkChildren: false,
            template: '<input  {{dataItem.disabled}} type=\'checkbox\' ng-click=\'populateControlInPrcsGeoLoction(dataItem)\' value=\'true\' />'
        }
    },riskInPrcsGeoLocationTreeConfig : {
        template: '{{dataItem.text}}',
        checkboxes: {
            checkChildren: false,
            template: '<input  ng-disabled=\'disabled#: item.id #\' type=\'checkbox\' ng-click=\'populateRisToProcesskGeoLoctionList(dataItem)\' value=\'true\' />'
        }
    }

2 个答案:

答案 0 :(得分:0)

可以使用$ broadcast或$ emit(冒泡,降低),但我认为一个更好的模式是将其转移到它自己的指令中。然后,您可以将该指令包含在您想要的任何html中。包含该指令的ng-click,每当用户单击该元素时,将显示该下拉列表。

但是如果没有更多的信息/代码,很难完全理解你的意图是什么,因为这只是一个配置对象,一个来自控制器的功能。但是这里有一个指向kindo树视图角度页面的链接以获取更多信息:http://demos.telerik.com/kendo-ui/treeview/angular

答案 1 :(得分:0)

由于您希望在其他应用程序和指令中使用updateGeoLoctionList,因此最佳解决方案是将其置于服务中。然后可以将该服务注入指令,控制器或其他服务中以供使用。