我在使用 ng-model 时遇到问题。当我查看该对象时,所有属性都是空的:
$scope.edit = {
name: " ",
gender: " ",
major: " ",
gpa: " ",
act: " ",
sat: " ",
toefl: " ",
primary: " ",
secondary: " ",
country: " ",
summary: " ",
past: " ",
media: " "
};
但是当我单击提交按钮时,它返回的所有内容都是
最佳答案
从plunker(虽然我无法运行你的代码)来看,很明显的是你在表单中的两个不同场合使用了相同的 Controller PlayerController
。
What happens here is that the method
updateUser()
of theng-click
attribute is bound to a new instance of the controllerPlayerController
. Therefore creating a newscope
, and totally unaware of what changes have been brought to its neighboring namesake controller's scope properties (in this caseedit
).
<div ng-controller="PlayerController">
<!-- ISOLATE region -->
<textarea ng-model="edit.someproperty"></textarea>
</div>
<div ng-controller="PlayerController">
<!-- Another isolate region unaware of the above -->
<button ng-click="updateUser(edit)">Update</button>
</div>
解决方案
将您的整个表单封装在一个 ngController
指令中,以便该范围内的所有内容(ngModel
指令和方法 updateUser
)使用相同的范围属性。
注意:我还注意到您已将 ngController
包装在 form
中,其中是不正确的。 AngularJS 中的 form
强加了一个 FormController
Controller 对象,并且该 Controller 成为 Controller 的直接父级。事实上,您使用 ngController 创建的 Controller 旨在处理表单的逻辑,而不是其他方式。
关于javascript - 文本区域上的 ng-model 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37065848/