gpt4 book ai didi

javascript - 我们应该使用什么来在 Angular JS 中分配模型数据?

转载 作者:行者123 更新时间:2023-11-29 14:42:37 26 4
gpt4 key购买 nike

我想知道使用 Angular JS 为每个表单元素分配模型名称的更好方法。

我可以使用单独的作用域变量来分配它。

<input type="text" ng-model="firstName"/>
<input type="text" ng-model="lastName"/>

我也可以使用单例对象分配它。

<input type="text" ng-model="contact.firstName"/>
<input type="text" ng-model="contact.lastName"/>

就内存和执行时间而言,哪种处理表单数据的方法更好?

最佳答案

您应该选择第二个选项,以避免范围继承问题,请参阅 the doc :

Scope inheritance is normally straightforward, and you often don't even need to know it is happening... until you try 2-way data binding (i.e., form elements, ng-model) to a primitive (e.g., number, string, boolean) defined on the parent scope from inside the child scope.

关于javascript - 我们应该使用什么来在 Angular JS 中分配模型数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36745041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com