从按钮上的另一个控制器调用函数onClick

时间:2017-09-06 08:44:08

标签: javascript angularjs

我试图点击一个按钮并触发另一个控制器的功能。这里的重要部分是我有两个控制器DashboardController,我有一个带按钮的数据表和一个onClick我调用另一个名为DashboardDeviceController的控制器的函数。

我这样做是因为当我点击按钮时,我将打开一个带有标签的新视图,其中每个标签都有图表。

所以我希望当我点击我的按钮指定它将打开而不是"消费"默认为活动标签。

DasboardController
datatableoptions中的

按钮

return icon = '<center><a class="state-link" data-state-id="' + data.id + '" ng-click="setActiveTab(\"consumptions\")"><i style="color:#ff0000; width:21px; height:21px" title="Consumptions threshold exceed" class="fa fa-warning"></i></a></center>';

我尝试这样做,但没有工作..

$rootScope.$on("setActiveTab", function(name, obj, event){
                $scope.parentmethod(name, obj);
            });

            $scope.parentmethod = function(name, obj, event) {
                // task
                console.log("name ",name);
                var btn = event.currentTarget;
                obj[btn.id]=true;
                console.log("btn ", btn);
                activeTab = name;
            }


DashboardDeviceController

controllerScope.activeTab = {
            consumptions: true,
            network     : false,
            ap          : false,
            modem       : false,
            system      : false,
        };


controllerScope.getActiveTab = function () {
            console.log("getActiveTab()");

            var activeTab = null;
            for(var tabName in controllerScope.activeTab) {
                if (controllerScope.activeTab[tabName]) {
                    activeTab = tabName;
                    break;
                }
            }
            return activeTab;
        }

        controllerScope.setActiveTab = function (name) {
            console.log("setActiveTab()");

            var activeTab = null;
            for(var tabName in controllerScope.activeTab) {
                if (controllerScope.activeTab[tabName] == name) {
                    controllerScope.activeTab = name;
                    break;
                }
            }
        }

4 个答案:

答案 0 :(得分:0)

在angularjs中,要拥有可重复使用的功能,您必须创建服务。

angular.module('myModule')
    .factory('myService', serviceFunction);

serviceFunction.$inject = [];

function serviceFunction() {
    return {
        reusableFunction1: function() {}
    };
}

在angularjs服务(或工厂)中实现应用程序的商务逻辑。例如,您可以使用服务来管理身份验证。创建服务后,您可以将其注入控制器以使用其功能:

angular.module('myModule')
    .controller('myCtrl', myCtrl);

myCtrl.$inject = ['$scope', 'myService'];

function myCtrl($scope, myService) {
    myService.reusableFunction1();
}

如果您想获得更多信息,我会加入服务和工厂官方文档的链接:

https://docs.angularjs.org/guide/services

https://docs.angularjs.org/guide/providers

答案 1 :(得分:0)

使用BROADCAST/EMIT an ON进行沟通

$scope.$emit('myCustomEvent', 'Data to send');

// firing an event downwards
$scope.$broadcast('myCustomEvent', {
  someProp: 'Sending you an Object!' // send whatever you want
});

// listen for the event in the relevant $scope
$scope.$on('myCustomEvent', function (event, data) {
  console.log(data); // 'Data to send'
});

示例

plunkr:https://plnkr.co/edit/WemGxPytLLmPulml8xjK?p=preview

您可以使用angular.extend在控制器之间进行通信/链接

app.controller('DashboardController', function($scope, $controller) {
        angular.extend(this, $controller('DashboardDeviceController', {$scope: $scope}));
});

答案 2 :(得分:0)

控制器之间的通信通过$ emit + $ on / $ broadcast + $来完成。

return icon = '<center><a class="state-link" data-state-id="' + data.id + '" ng-click="setActiveTab($event)"><i style="color:#ff0000; width:21px; height:21px" title="Consumptions threshold exceed" class="fa fa-warning"></i></a></center>';

setActiveTab($event)DashboardController的操作中,您需要调用DashboardDeviceController函数,如下所示

我们说你有

app.controller('DashboardDeviceController', ['$scope', '$rootScope'
    function($scope) {
        $rootScope.$on("CallParentMethod", function(){
           $scope.parentmethod();
        });

        $scope.parentmethod = function() {
            // task
        }
    }
]);
app.controller('DashboardController', ['$scope', '$rootScope'
    function($scope) {
        $scope.setActiveTab = function(event) {
            $rootScope.$emit("CallParentMethod", {});
        }
    }
]); 

答案 3 :(得分:0)

besat的想法是这样的, 首先,您需要检查控制器的优先级,例如 他们是沟通:

1.parent to child communication

then do in your parent controller $scope.$broadcast('name',{objkey:value});
then do in your child controller $scope.$on('name',function(event,args){
//console the args params
});

2.child与家长沟通:

then do in your child controller $scope.$emit('name',{objkey:value});
then do in your parent controller $scope.$on('name',function(event,args){
//console the args params
});