【问题标题】:value and ng-model are conflictedvalue 和 ng-model 冲突
【发布时间】:2015-08-31 02:12:12
【问题描述】:

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

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

//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-model 和 value 相互冲突,因此它不起作用。我也尝试了 ng-value ,但它也不起作用。这种情况有解决办法吗?

【问题讨论】:

  • 你能提供一个 plunker、jsfiddle 或类似的东西吗?

标签: javascript html angularjs data-binding


【解决方案1】:

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

$scope.document.edit.title

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

【讨论】:

    【解决方案2】:

    尝试设置

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

    并从输入中删除value=""

    【讨论】:

    • 谢谢。我现在使用 'data-ng-init="document.edit.title=document.title"'。
    • 这会起作用,但是最好将其设置在该范围的控制器中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多