0

1. Directive:

app.directive('inputField', function() {
    return {
        restrict: 'E',
        require: 'ngModel',
        scope: {
            words: '=ngModel'
        },
        transclude: true,

        template: "<input type='text' ng-model='words' placeholder='Translate' />"
    };
});

2. ng-click function:

$scope.clear = function() {
    $scope.words = { word: '' };
};

3. View looks like this:

<input-field id='inputWord' value='' name="data1" ng-model="words.word"></input-field>

After click clear()

{{words.word}}

and value in input still exist and $scope is broken.

Please tell me how I can clear all inputs ng-repeat and update scope?

Morteza QorbanAlizade
  • 1,520
  • 2
  • 19
  • 35
andrzej
  • 495
  • 1
  • 7
  • 18

1 Answers1

2

Try like this.

var app = angular.module('app',[])
app.controller('ctrl',function($scope){
  
  $scope.words = [ {word : 'input1'}, {word : 'input2'}, {word : 'input3'}];
  $scope.clear = function() {
          $scope.words =[ {word : ''}, {word : ''}, {word : ''}];
        };
});
app.directive('inputField', function() {
        return {
            restrict: 'E',
            require: 'ngModel',
            scope: {
                words: '=ngModel'
            },
            transclude: true,

            template: "<input type='text' ng-model='words' placeholder='Translate' />"
        };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <div ng-repeat="word in words">
  <input-field id='inputWord'  name="data1" ng-model="word.word"></input-field>
    </div>
  <button ng-click="clear()">Clear</button>
</div>
Hadi J
  • 16,989
  • 4
  • 36
  • 62
  • unfortunately after clear() $scope doesn't work. I mean when I type in one input it types in all inputs. – andrzej Sep 18 '16 at 09:58
  • Because your model for all input is same. each input should have unique model. – Hadi J Sep 18 '16 at 10:11
  • thanks, but this is not what I want, what if I don't know how many inputs I have? My input generates from database by ID count. – andrzej Sep 18 '16 at 11:35