简体   繁体   English

指令父单击中的角度指令

[英]angular directive in directive parent click

I'm creating a directive to show differences in text. 我正在创建一个指令以显示文本差异。 For this directive, I need a couple of buttons which I've split up in directives. 对于该指令,我需要几个在指令中分开的按钮。 A simpliefied example would be: 一个简单的例子是:

.directive('differenceViewer', function($templateCache, $compile) {
      return {
        restrict: 'E',
        scope: {
            oldtext: '@',
            newtext: '@',
            template: '@',
            heading: '@',
            options: '=',
            itemdata: '&',
            method: '&'
        },
        replace: false,
        link: (scope, element, attr) => {
            element.append(angular.element($compile($templateCache.get(scope.template))(scope)));
        }
    };
}).directive('diffbutton', function() {
  return {
        restrict: 'E',
        scope: {
            method: '&'
        },
        template: '<button class="btn btn-sm btn-success" ng-click="method()">Rollback</button>',
        replace: true,
        terminal: true,
        link: (scope, element, attr) => {
            scope.directiveClick = function(){
                console.log("directive method"); // is never called
            }

        }
    }
})

I compile the html via a template script: 我通过模板脚本编译html:

<script type="text/ng-template" id="differenceViewer.html">
    <div class="ibox-footer">
      <div class="row">
        <div class="col-md-12">
            <diffbutton method="clickedBtn()">Foo</diffbutton>
        </div>
      </div>
    </div>
</script>

Where the diffbutton is created inside the html compiled by differenceViewer . diffbutton在由differenceViewer编译的html内创建的位置。

I need to call a method in the controller that is responsible for creating all the difference-views. 我需要在控制器中调用一个方法,该方法负责创建所有差异视图。

app.controller('MainCtrl', function($scope) {
  $scope.clickedBtn = function() {
    console.log('foo'); // is never called
  }
})

Here's a plunker demonstrating the issue. 这是一个演示该问题的工具。

What do I need to change in order to be able to forward the button click on my directive in a directive to the controller method? 为了能够将按钮的点击转发到控制器方法的指令中,我需要更改什么?

I've been working with the answers of this question but still can't make it work. 我一直在研究这个问题的答案,但仍然无法解决。

Note that, if I add 请注意,如果我添加

scope.clickedBtn = function() {console.log("bar");}

to the differenceViewer directive, it gets called - however I need to call the method in the controller instead. differenceViewer指令,它会被调用-但是我需要改为在控制器中调用该方法。

Pass on a method from the parent to the child element and then trigger it on click. 将方法从父元素传递到子元素,然后在单击时触发它。 For example (pseudo code coming in ) 例如(伪代码进来)

<parent-directive>
   <child-directive totrigger="parentClickCallback()"></child-directive>
</parent-directive>

then in your parent directive you set 然后在您的父指令中进行设置

$scope.parentClickCallback = function(){//do whatever you neeed}

and on your child in its scope binding you set 并在您的孩子的范围绑定中设置

scope:{
   totrigger:'&'
}

and on that child button you simply add 然后在该子按钮上您只需添加

<button ng-click="totrigger()">ClickMe</button>

every time you click that button it will trigger parentClickCallback by reference attached to totrigger. 每次您单击该按钮时,都会触发附加到totrigger的引用来触发parentClickCallback。

Why would you need to complicate your code so much I'm not really sure. 我不确定为什么为什么要使您的代码如此复杂。 If you not happy with scope binding just require controller in your directive and pass the controller bound function. 如果您对范围绑定不满意,只需在指令中要求使用控制器并传递控制器绑定功能即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM