AngularJS 指令:需要 ngBind
AngularJS directive : require ngBind
我目前正在编写依赖于 ngBind 的属性指令。我需要该元素带有 ngBind 属性才能使指令正常工作。我在想一个简单的 require: 'ngBind'
就足够了,就像你对 ngModel
所做的那样。这就是我所做的:
app.directive( 'myDirective', function() {
return {
restrict: 'A',
require: 'ngBind',
link: function(scope, element, attrs) { .. }
});
下面是我如何使用我的指令:
<span my_directive="" ng_bind="valueToBeBound"></span>
但是后来我得到了这个错误,所以我想不能这样做:
Error: error:ctreq
Missing Required Controller
Controller 'ngBind', required by directive 'myDirective', can't be found!
有什么办法可以强制 ngBind
存在吗?
谢谢!
正如这里解释的那样:Angular NgModelController 你需要提供 ngBind
<span ng-bind="ModelName"></span>
这是预期的行为。正如指令的 require
选项的 AngularJS 文档中所定义的那样:
Require another directive and inject its controller as the fourth
argument to the linking function. The require takes a string name (or
array of strings) of the directive(s) to pass in. If an array is used,
the injected argument will be an array in corresponding order. If no
such directive can be found, or if the directive does not have a
controller, then an error is raised (unless no link function is
specified, in which case error checking is skipped).
由于 myDirective
所需的 ngBind
指令没有控制器,因此预计会出现错误,除非您删除 myDirective
中的 link 函数] 指令然后 angular 将简单地跳过错误检查。
有两种方法可以达到你想要的效果。
- 删除
myDirective
指令中的 link()
函数,然后在该指令中添加控制器函数以添加组件逻辑。此解决方案的问题是您无法在 link()
函数中附加 DOM 逻辑。
- 处理该问题的最理想方法是简单地删除
require
选项,并简单地检查 myDirective
指令所在的元素中是否存在 ngBind
属性.
例如
app.directive( 'myDirective', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
if(angular.isUndefined(attrs.ngBind)) {
return;
}
// Your myDirective DOM LOGIC/MANIPULATION should be here
}
});
我目前正在编写依赖于 ngBind 的属性指令。我需要该元素带有 ngBind 属性才能使指令正常工作。我在想一个简单的 require: 'ngBind'
就足够了,就像你对 ngModel
所做的那样。这就是我所做的:
app.directive( 'myDirective', function() {
return {
restrict: 'A',
require: 'ngBind',
link: function(scope, element, attrs) { .. }
});
下面是我如何使用我的指令:
<span my_directive="" ng_bind="valueToBeBound"></span>
但是后来我得到了这个错误,所以我想不能这样做:
Error: error:ctreq
Missing Required Controller
Controller 'ngBind', required by directive 'myDirective', can't be found!
有什么办法可以强制 ngBind
存在吗?
谢谢!
正如这里解释的那样:Angular NgModelController 你需要提供 ngBind
<span ng-bind="ModelName"></span>
这是预期的行为。正如指令的 require
选项的 AngularJS 文档中所定义的那样:
Require another directive and inject its controller as the fourth argument to the linking function. The require takes a string name (or array of strings) of the directive(s) to pass in. If an array is used, the injected argument will be an array in corresponding order. If no such directive can be found, or if the directive does not have a controller, then an error is raised (unless no link function is specified, in which case error checking is skipped).
由于 myDirective
所需的 ngBind
指令没有控制器,因此预计会出现错误,除非您删除 myDirective
中的 link 函数] 指令然后 angular 将简单地跳过错误检查。
有两种方法可以达到你想要的效果。
- 删除
myDirective
指令中的link()
函数,然后在该指令中添加控制器函数以添加组件逻辑。此解决方案的问题是您无法在link()
函数中附加 DOM 逻辑。
- 处理该问题的最理想方法是简单地删除
require
选项,并简单地检查myDirective
指令所在的元素中是否存在ngBind
属性.
例如
app.directive( 'myDirective', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
if(angular.isUndefined(attrs.ngBind)) {
return;
}
// Your myDirective DOM LOGIC/MANIPULATION should be here
}
});