棱角分明,难以让指令工作

Angular, trouble getting directive to work

本文关键字:指令 工作      更新时间:2023-09-26

我似乎无法让我的指令工作,我想我可能注册错误,但我似乎无法弄清楚如何。可能是命名约定?这是我所拥有的——

我的指令 -

'use strict';
angular.module('demoApp')
.directive('packeryAngular', ['$rootScope', '$timeout',
function($rootScope, $timeout) {
return {
  restrict: 'A',
  scope: true,
  link: function(scope, element, attrs) {
};
}
]);

在我的div 上,我只是这样称呼它

<div packeryAngular>

然而,当我这样称呼它时,似乎什么都没有发生。我的第一个假设是我需要像这样在我的应用程序上注册

angular.module('demoApp', [ 'packeryAngular ']

但是,当我这样做时,我在控制台中收到错误:

Error: [$injector:modulerr] Failed to instantiate module packeryAngular due to:
Error: [$injector:nomod] Module 'packeryAngular' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
所以,我

对此很陌生,但我不完全确定我在这里做错了什么,我试图复制其他工作示例,但似乎无法让它工作。将不胜感激任何帮助,非常感谢您的阅读!

不需要

注入指令,只需要在html中使用即可。带有大写字母的指令的第二个单词变成带有连字符的小单词,因此所有下一个大写字母的第一个字母将变小,并且前面会带有连字符,例如您的自定义指令成为您的自定义指令

<div packery-angular>

摆脱在模块中注入它

angular.module('demoApp', []);

您必须在节点上放置以下属性:

<div packery-angular>

注册指令时,您的姓名必须使用小驼峰大小写形式。但是HTML不能很好地处理属性名称中的大写字母,因此AngularJS希望大写字母替换为破折号,后跟相应的小写字母。

所以你在javascript代码中的指令名是yourDirectiveName的,但在HTML代码中,它必须被称为your-directive-name