Call method in directive controller from other controller

后端 未结 4 1821
轮回少年
轮回少年 2020-12-02 03:28

I have a directive that has its own controller. See the below code:

var popdown = angular.module(\'xModules\',[]);

popdown.directive(\'popdown\', function (         


        
4条回答
  •  难免孤独
    2020-12-02 04:02

    You can also use events to trigger the Popdown.

    Here's a fiddle based on satchmorun's solution. It dispenses with the PopdownAPI, and the top-level controller instead $broadcasts 'success' and 'error' events down the scope chain:

    $scope.success = function(msg) { $scope.$broadcast('success', msg); };
    $scope.error   = function(msg) { $scope.$broadcast('error', msg); };
    

    The Popdown module then registers handler functions for these events, e.g:

    $scope.$on('success', function(event, msg) {
        $scope.status = 'success';
        $scope.message = msg;
        $scope.toggleDisplay();
    });
    

    This works, at least, and seems to me to be a nicely decoupled solution. I'll let others chime in if this is considered poor practice for some reason.

提交回复
热议问题