user3519456
user3519456

Reputation:

Angular ng-model is undefined in JS

This is my code

HTML:

<div class="someClass">
  <label>Enter Text Here:</label>
  <textarea id="content-editable" ng-model="myTextArea" ng-keyup="callme($event)">
  </textarea>
</div>

JS:

var text = $scope.myTextArea.text

I am getting an exception in JS that "cannot find property text of undefined".

I am not sure if text property is needed to get the text from textarea but I am getting undefined in debugger for $scope.myTextArea

Whats wrong?

Upvotes: 1

Views: 98

Answers (2)

kukkuz
kukkuz

Reputation: 42352

Data binding require just to refer $scopel.myTextArea and you don't need to do .text - see demo below:

angular.module("app", []).controller("ctrl", function($scope) {
  $scope.callme = function(event) {
    console.log($scope.myTextArea);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="someClass" ng-app="app" ng-controller="ctrl">
  <label>Enter Text Here:</label>
  <textarea id="content-editable" ng-model="myTextArea" ng-keyup="callme($event)">
  </textarea>
</div>

Upvotes: 2

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You don't need to use .text cz it'll by default give you the value.

just use like this

var text = $scope.myTextArea;

Upvotes: 1

Related Questions