javascript - 为什么 Angular 文档在大多数情况下建议不要使用 ng-init?

标签 javascript angularjs

来自 the ng-init documentation :

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.



该文档继续给出以下示例:
<div ng-repeat="innerList in list" ng-init="outerIndex = $index">
    // [snip]
</div>

什么使使用更合适 ng-initng-repeat但不是,例如,使用 ng-model像这样:
<input ng-model="thing.prop"
       ng-init="thing.prop = thing.prop || 'defaultValue'">

该文档说应该“使用 Controller 而不是 ngInit”。在这种情况下, Controller 有什么好处?这是 Angular 的风格偏好,还是在某些情况下上述代码不起作用?

最佳答案

那是因为文档要尽量简洁,如果在每个例子中都创建了一个Controller,就不会那么精确和清晰

关于javascript - 为什么 Angular 文档在大多数情况下建议不要使用 ng-init?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418637/

相关文章:

javascript - Laravel 5.1 - 使用 ajax 打印评论,然后能够立即删除它

javascript - 如何从对象内部进行 .append?

html - 首先加载css,然后加载页面内容angularjs

javascript - Angular watch 并不总是有效

javascript - Firebase 查询的优化。通过id获取数据

php - Javascript 函数仅适用于其中的某些 "alert()"

javascript - three.js 在线包含它的 URL 是什么?

javascript - 为什么要使用 Array.prototype.forEach.call(array, cb) 而不是 array.forEach(cb)?

angularjs - 我应该总是通过 $push() 将新对象添加到对象列表中吗?

javascript - 如何用Javascript制作表格边框动画?