4

My HTML is as follows:

<textarea ng-model="cancelMessage"></textarea>
<span {{100 - cancelMessage.length}} characters remaining</span>

In my controller I declare a variable as follows:

$scope.cancelMessage = '';

When I type in the text area, the number of characters remaining actually updates. However, when I try to access

$scope.cancelMessage 

(via ng-click), the variable is still '' and not the text in the text area.

Hoa
  • 19,858
  • 28
  • 78
  • 107

2 Answers2

3

Had the same problem and this solved the problem for me:

Cannot get textarea value in angularjs

Basically Angular created a new scope. Use $parent.cancelMessage in your textarea ng-model attrbibute instead.

Community
  • 1
  • 1
Riegus
  • 31
  • 3
2

Created a fiddle for the problem. Hope you get an idea where you making the mistake.

Here is the controller function:

function test($scope){
    $scope.cancelMessage = '';
    $scope.clickTest = function(){
        alert($scope.cancelMessage);
    }
}
V31
  • 7,626
  • 3
  • 26
  • 44