简体   繁体   English

从指令模板调用具有es6样式的控制器类的方法

[英]call a method of controller class with es6 style from directive template

I want call a method of controller class with ES6 style from directive template like this : 我想从指令模板调用具有ES6风格的控制器类方法:

my directive : 我的指令:

import angular from 'angular';

function dpGrid() {
    return {
        restrict: 'E',
        scope: {
            options: '='
        },
        template: require('./grid.directive.html')
    }
}

export default angular.module('directives.dpGrid', [])
    .directive('dpGrid', dpGrid)
    .name;

a segment of my directive template : 我的指令模板的一部分:

<a class="btn btn-info" ng-click="delete(item)">delete</a>

and I want call delete() method of this controller from directive template : 我想从指令模板调用此控制器的delete()方法:

export default class userController {

    constructor($scope) {
        this.$scope=$scope;    
    }

    delete(item){

        console.log("item : ",item);
    }


}
userController.$inject = ['$scope'];

and I cant use controller attrib in directive like this : 我不能在这样的指令中使用控制器attrib:

controller:'userController'

because I want use this directive with multi controller 因为我想将此指令与多控制器一起使用

I think you should call your directive from your controller. 我认为您应该从控制器调用指令。 A directive can be used in controller or directly in DOM via attribute. 指令可以在控制器中使用,也可以通过属性直接在DOM中使用。

I don't see why you want to call a controller method in a directive. 我不明白为什么要在指令中调用控制器方法。 A directive is made for DOM manipulation. 制定了用于DOM操作的指令。

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

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