AngularJS Controller - Directive -> invokation of Controller function from directive

quma

I have a question concerning the visibility of function beween controllers and directives. I have a controller and a directive. The directive looks like this way

(function() {
'use strict';

angular
    .module('myproject.schedule')
    .directive('dirname', dirname);

function dirname() {
  var directive = {
        restrict: 'A',
        replace: true,
        scope: {
            currentDateScheduler: "=",
            ...
        },
        controller: DirnameController,
        controllerAs: 'vm',
        bindToController: true,
        templateUrl: ... directive.html

My controller looks like this:

(function() {
'use strict';

angular
    .module('myproject.schedule')
    .controller('MyController', MyController);
...

In the directive.html file I have a ng-click which invoked functions of my controller - and this works fine.

Actually now I am not sure why? I thought that a directive has its own namespace and the functions of my controller is not visible in ... directive.html.

Thanks a lot your help!

benfes

Controller scope is available to any directive that appears as a child within the DOM element on which the controller is declared. E.g.

<div ng-controller="ctrl1">
    <dirname></dirnam> <!-- this has access to ctrl1 scope -->
</div>

So if you were to use the directive within another controller it would have access to that controllers scope. This means that if the function doesn't exist in the controller that the directive declared under then the ng-click will do nothing.

In the directive you can declare a controller, anything declared in this controller will override the controller function of the same name within the directive. E.g.

angular.module('myApp',[])
.controller('myController',function($scope){
    $scope.clickMe = function(){
       alert('clicked from the controller');
    }
})
.directive('dirname', function(){
    return {
        controller: function($scope){
            $scope.clickMe = function(){ alert('clicked from directive'); };
        },
    };
});

controllers can also be nested. In this case the scope again has a top down effect, whereby a function defined in the top most controller is available to the dom elements enclosed in a child controller. Also if this child controller has the same function declared then these will override the functionality of the parent controller.

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS: Exeucting function from controller in a directive

From Dev

AngularJS How to call directive function from controller

From Dev

AngularJS: Exeucting function from controller in a directive

From Dev

Calling directive function from controller angularjs

From Dev

angularjs directive will not call controller function

From Dev

Angularjs directive call controller function

From Dev

Angularjs directive call controller function

From Dev

AngularJS: Access directive from controller

From Dev

angularjs: broadcast from directive to controller

From Dev

AngularJS communication from controller to directive

From Dev

AngularJS - Refresh Directive from controller

From Dev

angularjs: broadcast from directive to controller

From Dev

AngularJS variable from controller to directive

From Dev

AngularJS use Directive from controller

From Dev

Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

From Dev

angular calling controller function in from directive controller

From Dev

Angularjs controller function vs directive function

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Java

Call a controller function from a directive without isolated scope in AngularJS

From Dev

AngularJS Pass value from directive link function to controller

From Dev

angularjs design. calling a directive function from another controller

From Dev

AngularJS Directive: Attaching a function from the controller to ng-click

From Dev

Cant't get function in controller to trigg from angularJs directive

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

AngularJS calling from Parent to Child Directive controller function

From Dev

How to pass a value from an AngularJS directive to a function in parent controller

From Dev

AngularJS - Directive not recognizing controller

From Dev

AngularJS directive controller and require

From Dev

angularjs: access controller of directive

Related Related

  1. 1

    AngularJS: Exeucting function from controller in a directive

  2. 2

    AngularJS How to call directive function from controller

  3. 3

    AngularJS: Exeucting function from controller in a directive

  4. 4

    Calling directive function from controller angularjs

  5. 5

    angularjs directive will not call controller function

  6. 6

    Angularjs directive call controller function

  7. 7

    Angularjs directive call controller function

  8. 8

    AngularJS: Access directive from controller

  9. 9

    angularjs: broadcast from directive to controller

  10. 10

    AngularJS communication from controller to directive

  11. 11

    AngularJS - Refresh Directive from controller

  12. 12

    angularjs: broadcast from directive to controller

  13. 13

    AngularJS variable from controller to directive

  14. 14

    AngularJS use Directive from controller

  15. 15

    Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

  16. 16

    angular calling controller function in from directive controller

  17. 17

    Angularjs controller function vs directive function

  18. 18

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  19. 19

    Call a controller function from a directive without isolated scope in AngularJS

  20. 20

    AngularJS Pass value from directive link function to controller

  21. 21

    angularjs design. calling a directive function from another controller

  22. 22

    AngularJS Directive: Attaching a function from the controller to ng-click

  23. 23

    Cant't get function in controller to trigg from angularJs directive

  24. 24

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  25. 25

    AngularJS calling from Parent to Child Directive controller function

  26. 26

    How to pass a value from an AngularJS directive to a function in parent controller

  27. 27

    AngularJS - Directive not recognizing controller

  28. 28

    AngularJS directive controller and require

  29. 29

    angularjs: access controller of directive

HotTag

Archive