angular.js - angularjs {{}}疑问

浏览:27日期:2023-02-05

问题描述

在html的界面中 {{a}}为什么输出的是

html<span class='ng-binding ng-scope'>4</span>

而我期望的是

html4

是否有遇到过的大神帮忙解惑~

问题解答

回答1:

引自官网:

The ngBind attribute tells Angular to replace the text content of thespecified HTML element with the value of a given expression, and to update the text content when the value of that expression changes.

意思就是ngBind会告诉angular去将你写的表达式替换成一个特殊的html元素。

而这个特殊的html元素就是那个带class的span了,

双花括号{{}}和ngBind是一样的,实际上是一个directive,只在适用范围有稍许区别

官方文档ngBind

回答2:

AngularJs ng-bind 输出的HTML值肯定不会单单是4。要不然输出值的位置如何控制?

回答3:

a这个变量的值是怎么样被赋进去的?一般来说$scope.a=4;{{a}}的输出结果就是4的呀。

回答4:

谢邀。在angular的源码中可以看到 https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L1...其实就是为了给这个'顶级文本节点'绑定数据用的

相关文章: