Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS How to call directive function from controller

Tags:

angularjs

I was looking for a way to find out how to call a function inside a directive from the controller. I got the snip but hence I am new to Angular, the below code flow is not very clear. Anyone mind to explain how the code is working. Thanks.

// Directive 
<map set-fn="setDirectiveFn(theDirFn)"></map>

scope: { setFn: '&' },
link: function(scope, element, attrs) {
    scope.updateMap = function() {
       alert('inside updateMap()');
    }
    scope.setFn({theDirFn: scope.updateMap});
}

// Controller
<button ng-click="directiveFn()">call directive function</button>
function MyCtrl($scope) {
    $scope.setDirectiveFn = function(directiveFn) {
        $scope.directiveFn = directiveFn;
    };
}
like image 977
Monojit Sarkar Avatar asked May 11 '16 14:05

Monojit Sarkar


People also ask

How do I invoke a directive in AngularJS?

In addition to all the built-in AngularJS directives, you can create your own directives. New directives are created by using the . directive function. To invoke the new directive, make an HTML element with the same tag name as the new directive.

Which directive do we use to inform AngularJS about the parts controlled by it?

The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.

What is difference between controller and directive AngularJS?

A controller is usually used to contain and maintain the logic for your view, which gets bound to your view via $scope. A directive is something that you might use repeatedly and is called in your view directly through the directive name which you can pass in as an attribute.


1 Answers

Starting with the controller, this block creates a setDirectiveFn() method on the $scope object in your controller that takes a single parameter (directiveFn) and then uses that parameter to create a directiveFn() method on the $scope object in your controller.

    $scope.setDirectiveFn = function(directiveFn) {
        $scope.directiveFn = directiveFn;
    };

Inside the directive it is creating an updateMap() method on the scope object in the directive and then calling the setFn() method which is mapped to the $scope.setDirectiveFn() method by this line: <map set-fn="setDirectiveFn(theDirFn)"></map> in your HTML and this line: scope: { setFn: '&' } in your directive. It is passing the scope.updateMap() method which effectively sets $scope.directiveFn() in your controller equal to scope.updateMap() in your directive.

link: function(scope, element, attrs) {
    scope.updateMap = function() {
       alert('inside updateMap()');
    }
    scope.setFn({theDirFn: scope.updateMap});
}

The button is then calling $scope.directiveFn() in your controller which has been mapped to scope.updateMap() in your directive.

<button ng-click="directiveFn()">call directive function</button>
like image 70
Lex Avatar answered Oct 19 '22 19:10

Lex