AngularJS ng-value and ng-model do not work together [duplicate]

被刻印的时光 ゝ 提交于 2019-12-20 06:22:15

问题


I was trying to give a value to textfield using ngvalue and textarea has that value but it when I submit it, it recognized as textarea is empty.

 <textarea  ng-value="main.record.basic_info.application_letter"
            class="form-control" 
            ng-model="main.record.apply_job.cover_letter" 
            name="name" rows="15" cols="80" style="resize: none">
 </textarea>

回答1:


You cannot use these directives together

"It can also be used to achieve one-way binding of a given expression to an input element such as an input[text] or a textarea, when that element does not use ngModel."

You should set this value in your controller rather than use ng-value.




回答2:


The documentation clearly states that ng-value should not be used when doing two-way binding with ng-model.

From the Docs:

ngValue

Binds the given expression to the value of the element.

It can also be used to achieve one-way binding of a given expression to an input element such as an input[text] or a textarea, when that element does not use ngModel.

— AngularJS ng-value Directive API Reference

Instead, initialize the value from the controller:

<textarea  ̶n̶g̶-̶v̶a̶l̶u̶e̶=̶"̶m̶a̶i̶n̶.̶r̶e̶c̶o̶r̶d̶.̶b̶a̶s̶i̶c̶_̶i̶n̶f̶o̶.̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶_̶l̶e̶t̶t̶e̶r̶"̶
           class="form-control" 
           ng-model="main.record.name" 
           name="name" rows="15" cols="80" style="resize: none">
</textarea>
$scope.main.record.name = $scope.main.record.basic_info.application_letter;


来源:https://stackoverflow.com/questions/54453053/angularjs-ng-value-and-ng-model-do-not-work-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!