P
P
Pogran2016-12-13 14:58:52
Angular
Pogran, 2016-12-13 14:58:52

How to pass a function from a controller to a directive?

I have a function in a test controller. And I need to pass it to the directive and call it there.
directive looks like this

/** @ngInject */
    function segment() {
        return {
            replace: true,
            link: link,
            restrict: 'E',
            scope: {},
            bindToController: {
              segments: '=',
              type: '@'
            },
            templateUrl: 'app/audience/segment.template.html',
            controller: SegmentDirectiveController,
            controllerAs: 'ctrl'
        };

        function link(scope, el, attr, ngModel) {

        }
    }

    /** @ngInject */
    function SegmentDirectiveController() {
function removeSegment(segment, type) {
  // вот тут я хочу вызывать эту функцию.
}
}

How can I do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2016-12-13
@Pogran

{ 
  bindToController: {
    segments: '=',
    type: '@',
    onRemoveSegment: '&'
  }
}

function removeSegment(segment, type) {
  directiveController.onRemoveSegment({$someInformation: 'Segment ' + segment + ' removed'});
}

this.segmentRemoved = function($someInformation) {
  console.log('Callback function ivoked with', $someInformation);
};

V
Vladimir, 2016-12-13
@Casufi

scope: {
'close': '&onClose'
},
https://docs.angularjs.org/guide/directive

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question