gpt4 book ai didi

javascript - Angular - 指令和使用模块

转载 作者:搜寻专家 更新时间:2023-11-01 04:09:53 25 4
gpt4 key购买 nike

在研究了各种不同的项目并阅读了尽可能多的文档之后,我遇到了如何在我的应用程序中包含指令的问题。该应用程序设置如下:

app.js - 只是最上面的部分

angular.module('ngDashboard', ['ngCookies','ngResource','ngDashboard.filters', 'ngDashboard.services', 'ngDashboard.directives'])

所有模块都工作正常,除了(它是一个从示例重写的应用程序)对于根本不起作用的指令:

directives.js - 以下指令不起作用,也不会在 View 上执行指令:

angular.module('ngDashboard.directives', []).
directive('funkyElement', function () {
return {
restrict: 'E',
transclude: true,
scope: 'isolate',
template: '<div>gonna parse this: {{orig}} <br/>... and get this: {{obj}}</div>',
//templateUrl: 'template.html',
compile:function (element, attr, transclusionFunc) {
return function (scope, iterStartElement, attr) {
var origElem = transclusionFunc(scope);
var content = origElem.text();
scope.orig = content;
scope.obj = my_custom_parsing(content);
};
}
};
});

同一 directives.js 文件中的以下内容确实可以正常工作并且指令会执行:

angular.module('ng').directive('funkyElement', function () {
return {
restrict: 'E',
transclude: true,
scope: 'isolate',
template: '<div>gonna parse this: {{orig}} <br/>... and get this: {{obj}}</div>',
//templateUrl: 'template.html',
compile:function (element, attr, transclusionFunc) {
return function (scope, iterStartElement, attr) {
var origElem = transclusionFunc(scope);
var content = origElem.text();
scope.orig = content;
scope.obj = my_custom_parsing(content);
};
}
};
});

HTML 很简单:

<funky-element>
Parse me... come ooooon! Just parse meee!
</funky-element>

我的问题是,包含一组指令的正确方法是什么,也许为什么第一个示例(使用 ngDashboard.services)不起作用。

最佳答案

事实证明,我拥有的 app.js 文件要么已被缓存,因此指令依赖项不存在,要么我忽略了保存它(周末工作和深夜都可能发生)。由于这个问题在我更新 app.js 后得到解决,我将根据以下建议将其标记为已解决:

  1. 检查脚本控制台以确保您的文件未被缓存
  2. 完全关闭缓存,或使用隐身模式。
  3. 始终确保将 ng-app 添加到您的文档中(并非如此,但可以帮助其他人)
  4. 确保保存文件
  5. 累了就多喝咖啡,学习新的编程语言/框架。

最后,关于 Angular,我没有意识到您可以将指令添加到 ng 模块并且它们变得可用。我敢肯定这不是最佳做法,但对于测试和快速编写代码来说,它可能会派上用场。

关于javascript - Angular - 指令和使用模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16133682/

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