是否可以在angularjs中使用ng-model预填充表单?

Is it possible to prefill a form using ng-model in angularjs?

本文关键字:ng-model 填充 表单 angularjs 是否      更新时间:2023-09-26

我有一个简单的表单

 <form>
    Name:
    <input type="text" ng-model="edit"><br />
  </form>

和控制器

中的一个简单数组
$scope.statuses = [
    {value: 1, text: 'status1'},
    {value: 2, text: 'status2'},
    {value: 3, text: 'status3'},
    {value: 4, text: 'status4'}
  ]; 

我在视图中重复数组

<div ng-repeat="x in statuses">{{x.text}}<button ng-model="edit">edit</button></div>

所以我想知道当我点击编辑按钮可以使用ng-model在表单输入字段中填充数据吗?恰好

您可以通过在您的按钮中使用ng-click而不是ng-model来做到这一点。

你的按钮必须在点击时运行一个函数,就像这样:

<button ng-click="editFunction(x)">edit</button>

然后在控制器中,有一个函数:

$scope.editFunction = function(x){
   $scope.edit = x.text;
}

在这个函数中,你将得到x的值,并在你的表格ng-model中设置这个值。

有一个工作的活塞