为什么我们不能在“ng-model"中使用插值表达式?但是可以与“ng-src"一起使用吗? [英] Why can't we use interpolation expression with "ng-model" but can use with "ng-src"?

查看:21
本文介绍了为什么我们不能在“ng-model"中使用插值表达式?但是可以与“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-modelng-showng-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆