为什么我们不能将插值表达式与"ng-model"一起使用?但可以与"ng-src"一起使用吗? [英] Why can't we use interpolation expression with "ng-model" but can use with "ng-src"?
问题描述
我正在尝试将插值表达式与ng-model
指令一起使用,但是它不起作用.相反,当我将插值与ng-src
一起使用时,它可以很好地工作.这有什么区别?
I am trying to use the interpolation expression with the ng-model
directive but it doesn't work. On the contrary, when I use interpolation with ng-src
, it works perfectly fine. What is this difference due to?
推荐答案
这完全取决于指令的设置方式.
It all depends upon how the directive has been setup.
ng-model
,ng-show
和ng-click
之类的某些指令不使用插值符号,而指令ng-src
则采用插值符号.
Some directives like ng-model
, ng-show
and ng-click
do not use interpolation symbols whereas directives ng-src
take interpolation.
仅适用于字符串的指令支持插值.如果我们看一下ng-src的实现,您会发现
Interpolation are supported on directives that work only with strings. If we look at ng-src implementation you will find
attr.$observe(normalized, function(value) {
if (!value)
return;
attr.$set(attrName, value);
if (msie) element.prop(attrName, attr[attrName]);
});
attr.$observe
监视属性而不是模型的更改.模型更改会导致属性更改(由于插值),从而导致触发触发器.
attr.$observe
watches for change in attribute not model. Model changes cause attribute changes (due to interpolation), hence causing the trigger to fire.
对于所有其他指令,例如ng-model
,属性值是一个在当前范围内求值的表达式,并不限于字符串值.
For all other directives like ng-model
, the attribute value is an expression which is evaluated in current scope and is not limited to string value.
如果您要开发自己的指令,则隔离范围属性=
和@
可以帮助您实现类似的目标.
If you are developing your own directives, isolated scope properties =
and @
help you achieve something similar.
这篇关于为什么我们不能将插值表达式与"ng-model"一起使用?但可以与"ng-src"一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!