[英]Call link function directive Angular 1 ES6
I create a directive based on ES6 style: 我创建基于ES6样式的指令:
export default class myDirective {
constructor() {
this.restrict = 'E';
this.scope = {};
this.link = link;
}
link() {
console.log('link myDirective');
}
}
then in index.js
: 然后在
index.js
:
import angular from 'angular';
import myDirective from './myDirective';
export default angular
.module('app.directives', [])
.directive('myDirective ', () => new myDirective())
.name;
But when I call myDirective on html like: <my-directive><my-directive>
it does not call link function or compile function. 但是,当我在html上调用myDirective时,例如:
<my-directive><my-directive>
它不会调用链接函数或编译函数。 What can I do? 我能做什么?
We use ES6 here, and our directives dont really look like that, I'll give an example: 我们在这里使用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
});
You get the idea anyway. 无论如何,您都会明白。 Try structuring it like that and see if the link function works as you would expect.
尝试像这样构造它,然后查看链接功能是否按预期工作。
I have the same problem using AngularJS + ES6 + Webpack. 我在使用AngularJS + ES6 + Webpack时遇到相同的问题。 Maybe you could add this in your Directive, in your compile function:
也许您可以在指令的编译函数中添加以下内容:
compile() {
//console.log("compile");
return this.link.bind(this);
}
Check this links for more acurated info: 检查此链接以获取更多信息:
https://github.com/geniuscarrier/webpack-angular-es6/blob/master/app/modules/home/directive/footer.js https://github.com/geniuscarrier/webpack-angular-es6/blob/master/app/modules/home/directive/footer.js
https://www.michaelbromley.co.uk/blog/exploring-es6-classes-in-angularjs-1.x/ https://www.michaelbromley.co.uk/blog/exploring-es6-classes-in-angularjs-1.x/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.