Ruz*_*ard 5 angularjs angularjs-directive angularjs-scope
我正在尝试使用angular-http-auth库和bootstrap模式窗口.模态工作正常但我的指令有问题.这是一个jsfiddle链接 - http://jsfiddle.net/jCUSh/85/.我正在尝试添加一个将被调用并添加侦听器的指令.我在jsfiddle中简化了示例,因此您将看不到http-auth导入.但是scope.on('')元素仍然保留(无论如何它们都不会破坏图片).
我的问题是 - 为什么不调用链接函数?我添加elem.addClass('test')了一个例子.我相信解决方案非常简单,只是无法看到它.
同样不太重要的问题 - 将范围作为参数传递给另一个范围是否可以?我需要它来关闭模态窗口.
谢谢
大多数指令错误都显示在控制台中,只需启用日志记录:
app.config(function($logProvider){
$logProvider.debugEnabled(true);
});
Run Code Online (Sandbox Code Playgroud)
此外,您可以断言if指令是否实际加载:
angular.module('my', [])
.controller('Controller', [ '$scope', '$injector',
function ($scope, $injector) {
assertDirectives($injector, [ 'dir1', 'dir2']);
});
function assertDirectives($injector, directives){
_.each(directives, function(directiveCamelCase){
if( !$injector.has(directiveCamelCase + 'Directive') )
throw("Directive " + directiveCamelCase + " is not available.")
});
}
//you may replace underscore's `each` with jquery `each` or regular js loop
Run Code Online (Sandbox Code Playgroud)
因此,您无需猜测为什么指令不起作用.
这里有两件事在起作用..
一是您必须通过属性传递指令class,而不是通过ng-class
其次,"C"您传递给restrict属性的字符是ASCII为1057的字符(不是我们通常的ASCII 67字符)
修复了演示 http://jsfiddle.net/gaby/jCUSh/87/