值和 ng 模型是冲突的

value and ng-model are conflicted

本文关键字:冲突 模型 ng 值和      更新时间:2023-09-26

我正在研究一个有角度的形式,用户可以发送他们的数据更新并在需要时取消编辑。

发现我应该将编辑保存在一个单独的变量中。所以这里有一些代码:

//if editor is turned off it shows the saved value
<div data-ng-hide="editorEnabled" class="margin-bottom-top">{{document.title}}</div>
//if the editor is turned on it should save the edits in another variable to keep the older one
<input data-ng-show="editorEnabled" class="margin-bottom-top" data-ng-model="document.edit.title" value="{{document.title}}" type="text">

所以现在我可以访问两者 - 以前的标题和新编辑的标题。但是我希望以前的标题在输入字段中显示为值,以便用户可以看到当前值是什么,而不必再次输入它。使用占位符它可以工作,但这不是我想要的。当我检查我可以看到的元素时,正确的值是绑定的,但它没有显示。

我知道ng模型和值相互冲突,因此它不起作用。我也尝试了 ng 值,它也不起作用。这种情况有什么解决方案吗?

尝试设置

$scope.document.edit.title = $scope.document.title

并从输入中删除value=""

不要同时使用两者,将值设置为 ng-model

$scope.document.edit.title

它会自动为该输入设置值。