简体   繁体   English

Ng-Click不适用于Compile指令

[英]Ng-Click is not working in directive with Compile

I'm writing a fairly simple AngularJS directive which is a button. 我正在写一个相当简单的AngularJS指令,它是一个按钮。 The basic directive does look like: 基本指令的确如下所示:

officeButton.directive('officeImageButton', function() {
    return {
        restrict: 'E',
        replace: false,
        scope: {
            isDefault: '@',
            control: '=',
            label: '@',
            image: '@'
        },
        template: '<div class="button-wrapper" ng-click="onClick()">' +
                    '<a href="#" class="button image-button">' +
                      '<img src="{{image}}" />' +
                      '<span>{{label}}</span>' +
                    '</a>' +
                  '</div>',
       // Reset of the code not included for readability - See below.
    }
}];

Inside this directive, I do have a controller defined: 在此指令中,我确实定义了一个控制器:

/**
 * @description
 * Provides the controller for the 'officeImageButton' control. In this controller, all the required methods and
 * other information is stored.
 */
controller: ['$scope', function($scope) {
    // Allows an API on the directive.
    $scope.api = $scope.control || {};

    /**
     * @kind            Click
     * @name            onClick
     *
     * @description
     * This function is executed when the user click's the button itself.
     */
    this.onClick = function() {
        if (typeof $scope.api.onClick === 'function') { $scope.api.onClick(); }
    }
}],

And then I have my link function: 然后我有我的link功能:

link: function(scope, element, attributes, controller) {
    /**
     * @kind            Event
     * @name            onClick
     *
     * @description
     * Executes when the user click's the button.
     */
    scope.onClick = function() {
        controller.onClick();
    }
}

Since in the Template, I do have an ng-click attribute, the scope.onClick function is executed when I click the button. 由于在模板中,我确实具有ng-click属性,因此单击按钮时将执行scope.onClick函数。 This behaviour is expected. 此行为是预期的。

But now, in my directive, I need to also use the compile function in order to render the button correctly which is showed below: 但是现在,在我的指令中,我还需要使用compile函数才能正确呈现按钮,如下所示:

compile: function(element, attributes) {
    var floating = attributes['float'];
    // When there's floating, make sure to add the class 'floated' to the image.
    if (floating) { $('img', element).addClass('floated'); }
    // When there's right floating on the element, make sure to place the iamge after the <span> element.
    // In case of left floating, nothing needs to be changed.
    if (floating === 'right') {
        var imageElement = $('img', element);
        $(imageElement).remove();
        $('span', element).after(imageElement);
    }
},

But with this compile function included the ng-click isn't working anymore. 但是有了这个compile功能, ng-click不再起作用了。 Any toughts on what I'm doing wrong here? 我在这里做错了什么吗?

Kind regards 亲切的问候

A compile function's return value is the pre- and post- link functions, so when you define a compile property, the link property is ignored. compile函数的返回值是link前和link后的函数,因此定义compile属性时, link属性将被忽略。 And since you are not returning that link function in the compile, the scope.onClick is not on the scope. 并且由于您没有在编译中返回该链接函数,所以scope.onClick不在范围内。

To fix, you'd need to refactor a bit: 要解决此问题,您需要重构一下:

compile: function(tElem, tAttrs){

   // whatever you do now

   return function link(scope, element, attrs, ctrl){
     scope.onClick = function() {
        ctrl.onClick();
     }
}

Off-topic: 无关:

Also, note that you don't need to create an onClick in the controller. 另外,请注意,您无需在控制器中创建onClick The controller's use in the directive is to act as an API to other directives that require it. 该指令中控制器的用途是充当其他require该指令的指令的API。

I presume that you did mean to have officeImageButton.onClick to be invoked like that by another directive? 我认为您的确意味着要通过另一个指令来调用officeImageButton.onClick If you did, that's fine - but otherwise it is redundant - just use the link function to define elements on the scope. 如果您这样做了,那很好-但否则就多余了-只需使用link函数在作用域上定义元素即可。

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

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