如何设置ng-model的默认值?

时间:2019-01-18 11:02:43

标签: angularjs mean-stack

我这里有一个用于发布简单数据的表单,我想为输入提供默认的字符串值,但是为什么没有出现,我尝试了ng-value,但它也无法正常工作。如何为ng-model设置默认字符串?

<form name="regForm" ng-submit="register.regUser(regData);">

<input type="text" ng-model="register.regData.branch_name" name="branch_name" value="This is a string">   
<button type="submit">Submit</button>

</form>

2 个答案:

答案 0 :(得分:1)

使用ng-init指令设置默认值,如下所示:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
<form name="regForm" ng-submit="register.regUser(regData);">

<input type="text" ng-model="register.regData.branch_name" name="branch_name"  ng-init="register.regData.branch_name='This is a string'">   
<button type="submit">Submit</button>

</form>
</div>

答案 1 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
<form name="regForm" ng-submit="register.regUser(regData);">

<input type="text" ng-model="register.regData.branch_name" name="branch_name"  ng-init="register.regData.branch_name='This is a string'">   
<button type="submit">Submit</button>

</form>
</div>