Search code examples
javascriptangularjsscopetextareaangular-ngmodel

Getting value of TextArea angularjs


I have a textarea:

<textarea class="notes" ng-model="notes" placeholder="Some text" style="height: 630px;"></textarea>

I then have a button:

<button ng-click="saveNotes(notes)"></button>

I then have this method in my controller:

$scope.saveNotes = function(notes) {
    console.log(notes);
    student.saveNotes(notes, function() {
        console.log("Successfully saved notes.")
    }
)};

From what I can tell, notes should return the value of the textarea yet it is consistently returning "undefined". Can anyone spot the problem?

Thanks


Solution

  • Perhaps try assigning your variables to the controller rather then the scope like this

    <div ng-controller='myController as app'>
    <textarea class="notes" ng-model="app.notes" placeholder="Some text" style="height: 630px;"></textarea>
    <button ng-click="app.saveNotes(app.notes)"></button>
    </div>
    

    Then in your code under your controller

    this.saveNotes = function(notes) {
    console.log(notes);
    student.saveNotes(notes, function() {
        console.log("Successfully saved notes.")
    }
    )};
    

    In my opinion it just helps prevent conflicting scopes