2016-11-16 57 views
0

我创建基于ES6风格指令:呼叫链接功能指令角1 ES6

export default class myDirective { 
    constructor() { 
     this.restrict = 'E'; 
     this.scope = {}; 

     this.link = link; 
    } 
    link() { 
     console.log('link myDirective'); 
    } 
} 

然后在index.js

import angular from 'angular'; 

import myDirective from './myDirective'; 

export default angular 
       .module('app.directives', []) 
       .directive('myDirective ',() => new myDirective()) 
       .name; 

但是,当我打电话myDirective对HTML这样的:<my-directive><my-directive>它不调用链接函数或编译函数。我能做什么?

+0

你能尝试删除this.link =链接;来自构造函数。 –

+0

我试过但不起作用:/ –

+0

恩,它对我来说工作正常。唯一的问题是,你的指令有一个空格字符的名称,打破了声明(“myDirective”,删除空间),尽管如此,它为我工作。 –

回答

0

我们使用ES6这里,我们的指令不真正的样子,我就举一个例子:

import templateUrl from './some.html'; 
import SomeController from './someController'; 
export default() => ({ 
    templateUrl, 
    controller: SomeController, 
    controllerAs: 'vm', 
    scope: { 
    someVariable: '=' 
    }, 
    link: (scope, element, attrs, controller) => { 
    scope.link = { 
     someFunction: function some() { } 
    } 
    }, 
    bindToController: true 
}); 

你的想法呢。尝试像这样构建它,看看链接函数是否按照您的预期工作。

+0

嘿rrd,谢谢你!我会试试! –