即使 ng 模型有值,也使输入字段为空

Make input field empty even if ng model has a value

本文关键字:输入 字段 ng 模型 即使      更新时间:2023-09-26
            <div ng-if="item.type === 'textAnswer'">
                <label class="item item-input">
                    <span class="input-label">Answer:</span>
                    <input type="text"
                           ng-model="item.givenAnswer">
                </label>
            </div>

Item.givenAnswer在页面加载时输出"测试"。我该怎么做才能将答案保留在item.givenAnswer中,但确保输入在页面加载时为空?

基本上,用户可以在需要时更新他们的答案,而不会从他们以前的输入中分心。

你可以尝试像下面这样$watch,

   $scope.$watch(funtion(){return item.givenAnswer;}, function(newValue, oldValue) {
         scope.someothervariable = newValue;
         // Do database update here...
    });
您可以使用

ng-init方法在加载页面时将输入初始化为空白。这将更改范围内的值,但会保持数据库完好无损。

您可以向其添加更多逻辑,例如,如果以前的值仅为"测试",则将字段设置为空。

示例:http://plnkr.co/edit/EtNfcymqt6wJBV9Bcqdw?p=preview

延伸阅读:https://docs.angularjs.org/api/ng/directive/ngInit