1

I'm having some problems with emptying my input fields after they have been added with ng-repeat.

My controller looks like this:

var SimpleController = function ($scope) {
    $scope.rows = [{id: 'row1' }, {id: 'row2'}, {id: 'row3'}, {id: 'row4'}];

    $scope.addText = function () {
        $scope.modelRow = "text";   
    }
    $scope.removeText = function () {
        $scope.modelRow = " ";   
    }
}

I'm using ng-repeat on the rows array to add the amount of input fields i need. For example I have function that extends the array if the user need more fields.

Then i have a function to empty the inputs and one to fill them, which works like intended. However, if you type something in the input fields yourself, then neither of the functions won't work on that input.

My HTML:

<div ng-repeat="row in rows" >
    <input ng-model="modelRow" type="text" name="text" />
</div>

<button ng-click="addText()">Add text</button>
<button ng-click="removeText()">Remove Text</button>

I'm pretty new to Angular, so i'm probably missing something. Hopefully someone can help me out.

Also made a jsfiddle to demonstrate my problem, which can be found here: jsfiddle (Try typing something and you will see that the functions doesn't work).

2
  • You have more than one modelRow, and that is a problem. Commented Oct 29, 2015 at 11:18
  • I see. Is there any good solution to make this work? Commented Oct 29, 2015 at 11:23

2 Answers 2

3

Just elaborating the previous answer. Here is the controller you can have.

var SimpleController = function ($scope) {
    $scope.rows = [{id: 'row1', value: ''}, {id: 'row2', value: ''}, {id: 'row3', value: ''}, {id: 'row4', value: ''}];

    $scope.addText = function () {
        for (var i in $scope.rows) {
            $scope.rows[i].value = "text";
        }
    };
    $scope.removeText = function () {
         for (var i in $scope.rows) {
            $scope.rows[i].value = "";
        }
    }
};

Each row model should have a property to store the user's entered text. The functions addText() and removeText() should iterate overs the rows model and modify the value property.

<div ng-repeat="row in rows">
    <input ng-model="row.value" type="text" name="text"/>
</div>

<button ng-click="addText()">Add text</button>
<button ng-click="removeText()">Remove Text</button>
Sign up to request clarification or add additional context in comments.

1 Comment

That seems to do it. Thanks alot!
2

HTML

<div ng-repeat="row in rows" >
        <input ng-model="model.modelRow" type="text" name="text" />

    </div>

<button ng-click="addText()">Add text</button>
<button ng-click="removeText()">Remove Text</button>

controller

$scope.rows = [{id: 'row1' }, {id: 'row2'}, {id: 'row3'}, {id: 'row4'}];
    $scope.model={};
    $scope.model.modelRow='';
    $scope.addText = function () {
        $scope.model.modelRow = "text";   
    }
    $scope.removeText = function () {
        $scope.model.modelRow = " ";   
    }

changing code like this will solve your problem

Edit

in the above code if you type in one text field all text field will be filled with same content

if you use single model it is supposed to work like that

what i would suggest is to change the ng-model="row.value"

HTML

    <div ng-repeat="row in rows" >
        <input ng-model="row.value" type="text" name="text" />
    </div>

    <button ng-click="addText()">Add text</button>
    <button ng-click="removeText()">Remove Text</button>
    <!-- <pre>{{rows}}</pre> -->

Controller

$scope.rows = [{id: 'row1' }, {id: 'row2'}, {id: 'row3'}, {id: 'row4'}];

    $scope.addText = function () {
        for(var index in $scope.rows){
        $scope.rows[index].value="text";  
        }

    }
    $scope.removeText = function () {
         for(var index in $scope.rows){
         delete $scope.rows[index].value;  
        }
    }

2 Comments

Yeah, my intention with this is not to have the text fields filled with the same content. They should be individual
check the code in edit section , i think this will fulfill your requirement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.