Initially, the re...">

The default textarea value in angularjs when textarea is model bound

HTML:

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

Initially, the reason for the ban is empty. However, I want to specify a default value. But

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

Does not work.

Any ideas how to do this?

+4
source share
3 answers

Inside your controller, you can do the following:

 $scope.user = { ban_reason:"reason" } 
+7
source

Your angular application initializes when the DOM documents are ready - thus overriding the value using the value of $scope.user.ban_reason .

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

 $scope.user = { ban_reason: 'reason' }; 
+3
source

Well, if there is a situation that you still need for the default text to be used in the template, you can use ng-init.

  <textarea ng-model="text" ng-init="text = 'Hello, this is my default text.'"></textarea> 
+3
source

Source: https://habr.com/ru/post/1497033/


All Articles