为什么我们不能将插值表达式与"ng

编程入门 行业动态 更新时间:2024-10-21 17:28:25
本文介绍了为什么我们不能将插值表达式与"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-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

本文发布于:2023-10-26 23:35:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   能将   插值   ng   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!