Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default textarea value in angularjs when textarea is bound to model

HTML:

<textarea ng-model="user.ban_reason"></textarea>

Initially the ban reason is empty. However I want to provide a default value. But

<textarea ng-model="user.ban_reason">reason</textarea>

Does not work.

Any ideas how to do that?

like image 455
UpCat Avatar asked Aug 14 '13 13:08

UpCat


3 Answers

Inside your controller you can do as below:

$scope.user = {
    ban_reason:"reason"
}
like image 186
AlwaysALearner Avatar answered Sep 30 '22 19:09

AlwaysALearner


Well if there is a situation you still need it to have default text in the template, then you can use ng-init.

  <textarea ng-model="text" ng-init="text = 'Hello, this is my default text.'"></textarea>
like image 27
Lukas Jelinek Avatar answered Sep 30 '22 17:09

Lukas Jelinek


Your angular app initializes when the documents DOM is ready - thus, overrides the value with your $scope.user.ban_reason value.

In your controller, define a default value for the model property:

$scope.user = { ban_reason: 'reason' };
like image 25
fjdumont Avatar answered Sep 30 '22 18:09

fjdumont