angular.js : how to pass ngclick from original dom to directive's dom?

后端 未结 3 1695
情书的邮戳
情书的邮戳 2021-02-04 13:26

Hi there I have this \"confirmable\" button directive which I am working on,

The html code that will trigger the directive \'confirmable\'

      

        
3条回答
  •  旧巷少年郎
    2021-02-04 13:58

    It sounds to me like you want to call a method from a parent scope from within your directive...

    I've put together a Plunk here

    (Sorry, I like JavaScript... so here goes)

    Here's you're parent controller.

    app.controller('ParentCtrl', function($scope) {
        $scope.fooCalled = 0;
        $scope.foo = function() {
            $scope.fooCalled++;
        };
    });
    

    Then your mark up

    Foo Called: {{fooCalled}}

    And your directive declaration:

    app.directive('customControl', function(){
       return {
         restrict: 'E',
         scope: {
            innerFoo: '&customClick'
         },
         template: ''
       };
    });
    

    The bit in the scope declaration in your directive definition is what ties the parent scope function reference to your directive's scope so it can be called on click. That's what the & is for there.

提交回复
热议问题