[英]AngularJS Evaluate a Attribute directive after a Element directive
[英]AngularJS custom attribute does not evaluate first time directive runs
<a custom-attr='{{ controller.object.value }}' data-ng-model='controller.object.value'>
.directive('customAttr', function () {
return {
require: 'ngModel',
controller: 'ControllerName',
controllerAs: 'cName',
link: function (scope, el, attr, ctrl) {
el.on('click', function ($event) {
if (ctrl.$viewValue && attr.customAttr) { // breakpoint
}
})
}
}
})
在指令第一次运行时在attr.customAttr
看到正确的值。
在指令的链接函数内部的if语句的断点处停止时,我希望看到一个布尔值。 我已经使用$log.log()
验证了布尔值在模型中是正确的。 不幸的是,该指令第一次运行时, attr.customAttr
对模型值的引用的字符串(调试器中为'controller.object.value'
),然后在该指令的后续迭代中,它会正确计算为布尔值。 我尝试从属性中删除花括号,而我得到的是不变的空字符串。
我该怎么办才能使模型值在第一时间正确评估?
注意:之前,我已经完成了与此类似的版本,并使用了数字值,没有问题。 关键的区别似乎是工作版本在输入元素上,并且具有ngModel和ngValue属性。
事实证明,controller和controllerAs属性对此产生了影响。 我删除了它,但随后决定我希望拥有控制器,因此我改用了隔离作用域来评估对象,而不是从attr属性中读取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.