简体   繁体   English

Angularjs清除输入文本字段

[英]Angularjs clear input text field

I have this situation: 我有这种情况:

http://jsfiddle.net/f8erG/48/ http://jsfiddle.net/f8erG/48/

With some input text. 有一些输入文字。 When i fill the input i can hide them by a button. 当我填充输入时,我可以通过按钮隐藏它们。 What i need is that when the input hides all content inside that was typed clear. 我需要的是,当输入隐藏所有输入内容的内容时。 So when i click "Show" button the input must be empty. 因此,当我单击“显示”按钮时,输入必须为空。 I can't using ngIf before someone ask me. 在有人问我之前我不能使用ngIf。

This is the code: 这是代码:

<div ng-controller="myCtrl">
    <button ng-click="hideStuff()">Hide!</button>
    <button ng-click="showStuff()">Show!</button>
    <div ng-repeat="item in inputFileds">
        <input placeholder="{{item.label}}" class="default" ng-hide="hidden" ng-class="{'fade': startFade, 'show': !startFade}" type="text" ng-model="item.value" />
    </div>
</div>

And javascritp 和javascritp

var myApp = angular.module('myApp', []);

myApp.controller("myCtrl", function($scope, $timeout) {
    $scope.hideStuff = function() {
        $scope.startFade = true;
        $timeout(function() {
            $scope.hidden = true;
        }, 700);

    };

    $scope.showStuff = function() {
        $scope.startFade = false;
        $timeout(function() {
            $scope.hidden = false;
        }, 700);

    };


    $scope.inputFileds = [{
        "label": "input1",
        "value": ""
    }, {
        "label": "input2",
        "value": ""
    }, {
        "label": "input3",
        "value": ""
    }, {
        "label": "input4",
        "value": ""
    }];
});

You can avoid such problems with the right design! 您可以通过正确的设计避免此类问题!

Why do you put configuration data (labels) into the model? 为什么要将配置数据 (标签)放入模型中? Separate them into 2 object, because the labels are static, but the input field values are not. 将它们分成2个对象,因为标签是静态的,但输入字段值不是。 You can then just reset the modal very simple. 然后你可以很简单地重置模态。

$scope.model = {};

That's it - not need to reset every single field! 就是这样 - 不需要重置每一个字段!

Try to reinitialize the input fields:-> http://jsfiddle.net/maralik/f8erG/56/ 尝试重新初始化输入字段: - > http://jsfiddle.net/maralik/f8erG/56/

$scope.showStuff = function () {
    $scope.initInputFields();
    $scope.startFade = false;
    $timeout(function(){
        $scope.hidden = false;
    }, 700);

};

$scope.initInputFields = function() {
    $scope.inputFileds = [{
            "label": "input1",
            "value": ""
        },{
            "label": "input2",
            "value": ""
        },{
            "label": "input3",
            "value": ""
        },{
            "label": "input4",
            "value": ""
        }];        
};

$scope.initInputFields();

I added 我补充道

for(var i = 0; i < $scope.inputFileds.length; ++i) {
        $scope.inputFileds[i].value = "";
    }

in your hide function. 在你的隐藏功能中。

Its important to understand, that angular uses double binding in this case. 重要的是要理解,在这种情况下,角度使用双重绑定。 That means, if you update your model inside the controller, it will be reflected back. 这意味着,如果您在控制器内更新模型,它将被反射回来。 Thats why you can change the model object and the form will show an empty input field. 这就是为什么你可以改变模型对象和表单将显示一个空输入字段。

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

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