简体   繁体   English

禁用时基于AngularJS模型的输入值为空

[英]AngularJS model based input value empty when disabled

AngularJS client application. AngularJS客户端应用程序。

I have a form with two text inputs and a drop down listbox. 我有一个带有两个文本输入和一个下拉列表框的表单。 Only one of the two text inputs is enabled, the selected item in the listbox determines which input is enabled. 只有两个文本输入之一被启用,列表框中的选定项决定了哪个输入被启用。

The two text inputs are associated with fields in the model. 这两个文本输入与模型中的字段相关联。 However, when a text input is disabled, I would like the value to be cleared, not visible in a disabled state. 但是,当禁用文本输入时,我希望清除该值,在禁用状态下不可见。 When the input is enabled again, the model value should be displayed. 再次启用输入后,应显示模型值。

What is the best way to achieve this with AngularJS? 用AngularJS实现此目标的最佳方法是什么?

Angular Expression can be used with ngDisabled directive. Angular Expression可与ngDisabled指令一起使用。 Use ternary operator to check if selected value is equal to name of the input field and based on that set ngDisabled. 使用三元运算符检查所选值是否等于输入字段的名称,并基于该设置ngDisabled。 If the expression is truthy, then the disabled attribute will be set on the element. 如果表达式为真,则将在元素上设置Disabled属性。

Something like this in your html template. html模板中类似这样的内容。

ngDisabled = "($scope.input_model_variable_name !=    $scope.select_model_variable_name)?true:false"

One way to go about it would be 一种解决方法是

Call a function on ng-change on select. 调用ng-change on select上的函数。

-- copy all the values to restore later -复制所有值以供日后还原

-- blank all the text fields except for the field you want to edit -将所有文本字段保留为空白,但要编辑的字段除外

Have a function for ng-disable 具有ng-disable功能

-- disable the not selected text box -禁用未选中的文本框

-- enable the rest -启用其余

var app = angular.module('testApp', []);
app.controller('MainCtrl', function ($scope) {

    var modelCopy = {
        inputTextField1: "",
        inputTextField2: ""
    };

    $scope.data = {
        inputTextField1: "",
        inputTextField2: "",
        selectedInputField: ""
    };

    $scope.inputFieldChanged = function () {

        angular.forEach(modelCopy, function (value, key) {
            modelCopy[key] = $scope.data[key];
            if (this.selectedInputField === key) {
                this[key] = value;
            } else {
                this[key] = "";
            }
        }, $scope.data);

    };

    $scope.toggleDisability = function (fieldToEnable) {
        if ($scope.data.selectedInputField === fieldToEnable) {
            return false;
        } else {
            return true;
        }
    };
});

HTML 的HTML

<div ng-controller="MainCtrl">
    <div class="container">
        <form novalidate="novalidate" class="form-horizontal">
            <div class="form-group">
                <label for="selectInputFieldToEdit" class="control-label col-sm-2">Select Input Field to Edit</label>
                <div class="col-sm-6">
                    <select class="form-control" ng-change="inputFieldChanged()" id="selectInputFieldToEdit" ng-model="data.selectedInputField">
                        <option value="inputTextField1">Field 1</option>
                        <option value="inputTextField2">Field 2</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputTextField1" class="control-label col-sm-2">Input Text Field 1</label>
                <div class="col-sm-6">
                    <input id="inputTextField1" type="text" ng-model="data.inputTextField1" name="inputTextField1" class="form-control" ng-disabled="toggleDisability('inputTextField1')" />
                </div>
            </div>
            <div class="form-group">
                <label for="inputTextField2" class="control-label col-sm-2">Input Text Field 2</label>
                <div class="col-sm-6">
                    <input id="inputTextField2" type="text" ng-model="data.inputTextField2" name="inputTextField2" class="form-control" ng-disabled="toggleDisability('inputTextField2')" />
                </div>
            </div>
        </form>
    </div>
</div>

Here is a jsFiddle https://jsfiddle.net/hheymu/jpydjuhp/ 这是一个jsFiddle https://jsfiddle.net/hheymu/jpydjuhp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM