html - ng-init 和 ng-bind 有什么区别?

标签 html angularjs

下面是使用 ng-init 的代码:

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
  <p>Looping with ng-repeat:</p>
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

</body>
</html>

下面是使用 ng-bind 的代码:

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="" ng-bind="names=['Jani','Hege','Kai']">
  <p>Looping with ng-repeat:</p>
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

</body>
</html>

两者都提供相同的输出,但格式不同。

这里到底发生了什么?

请帮忙!

最佳答案

目的ng-init是在模板中初始化一个属性。

目的ng-bind是用给定表达式的值替换指定 HTML 元素的文本内容。

但是您应该在 Controller 中初始化变量并使用 {{variable}} 并且不要为此使用 ng-init 或 ng-bind。

关于html - ng-init 和 ng-bind 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38121316/

相关文章:

html - 2 列内容不同,但高度相同

AngularJS 包含没有外部 html

javascript - 在多个选择之间移动项目

javascript - 在工厂设置数据并将其绑定(bind)到 Controller

javascript - Angularjs Bootstrap 日期选择器 : Select multiple dates

html - IE11 中的 CSS3 动画问题

c# - MVC : Ace editor post

html - 多行的动画文本下划线(从左到右绘制动画)

html - 如何开发覆盖 100% 屏幕大小的 2 个 div CSS3 - 水平

css - Kendo Menu dataSource imageUrl 不接受内容