简体   繁体   中英

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 :

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 :

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 :

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.

I don't see why you want to call a controller method in a directive. A directive is made for DOM manipulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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