2

I have textarea with ng-model 'wordset' and ng-change="onChange()"

<div>
    <textarea ng-model="wordset" ng-change="onChange()"
              class="form-control app-word-set"
              placeholder="Enter Word Set" rows="4">
    </textarea>
</div>

I have button which added new textarea in this div. I needed that already added textarea includes the same on change method that my first textarea i have. But it should use ng-model... I want to use on method in my angularJS controller that gets values from every textarea by foreach like this:

$scope.wordSetTextarea = angular.element(document.getElementsByClassName('app-word-set'));

$scope.onChange = function() {
angular.forEach($scope.wordSetTextarea, function(value, key) {
      console.log(value);
   });
}

Is this possible?

1 Answer 1

2

With the AngularJS framework, multiple elements are added with the ng-repeat directive:

<div ng-repeat="item in itemArr">
    <textarea ng-model="item.wordset"
              ng-change="onChange(item,$index)"
              name="'item' + $index"
              class="form-control app-word-set"
              placeholder="Enter Word Set" rows="4">
    </textarea>
</div>

<button ng-click="addNewTextarea()">Add input</button>
$scope.itemArr = [{}];

$scope.addNewTextarea = function() {
    $scope.itemArr.push({});
};

New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes, so [data hiding problems] often shows up when these directives are involved ... [they] can be easily avoided by following the "best practice" of always have a '.' in your ng-models.

For more information, see

Sign up to request clarification or add additional context in comments.

Comments

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.