gpt4 book ai didi

javascript - Angular 1.5(组件): how to use directives with es6?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:47 25 4
gpt4 key购买 nike

在文件夹 directives 中我创建了两个文件:directives.jscolor.js

指令 我已经导入到 app.js 中

directives.js:

import angular from 'angular';

import ColorDirective from './color';

const moduleName = 'app.directives';

angular.module(moduleName, [])

.directive('color', ColorDirective);

export default moduleName;

color.js

import angular from 'angular';


let ColorDirective = function () {

return {
link: function (scope, element) {
console.log('ColorDirective');
}
}

}

export default ColorDirective;

在组件中的一个元素上,我添加了 color 作为 attr。

但它不起作用。我的意思是内部链接循环。为什么?我有什么错误的编码?如何在 angular 1.5 和 es2016 中使用指令?

最佳答案

从您所写的内容来看,不可能看出问题所在。您提供的代码有效,前提是您已将模块包含到页面中并且代码已正确编译。

我已将您的代码放入 fiddle 中,https://jsfiddle.net/fccmxchx/

let ColorDirective = function () {
return {
link: function (scope, element) {
console.log('ColorDirective');
element.text('ColorDirective');
}
}
}

angular.module('app.directives', [])
.directive('color', ColorDirective);

不幸的是我不能将你的代码拆分成模块,但这正是你的代码试图做的

关于javascript - Angular 1.5(组件): how to use directives with es6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com