javascript 当 textarea 绑定到模型时,angularjs 中的默认 textarea 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18232925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default textarea value in angularjs when textarea is bound to model
提问by Upvote
HTML:
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?
任何想法如何做到这一点?
回答by AlwaysALearner
Inside your controller you can do as below:
在您的控制器中,您可以执行以下操作:
$scope.user = {
ban_reason:"reason"
}
回答by Lukas Jelinek
Well if there is a situation you still need it to have default text in the template, then you can use ng-init.
好吧,如果在某些情况下您仍然需要它在模板中包含默认文本,那么您可以使用 ng-init。
<textarea ng-model="text" ng-init="text = 'Hello, this is my default text.'"></textarea>
回答by fjdumont
Your angular app initializes when the documents DOM is ready - thus, overrides the value with your $scope.user.ban_reason
value.
当文档 DOM 准备好时,您的 angular 应用程序会初始化 - 因此,用您的$scope.user.ban_reason
值覆盖该值。
In your controller, define a default value for the model property:
在您的控制器中,为模型属性定义一个默认值:
$scope.user = { ban_reason: 'reason' };