如何使用ng-click将元素添加到表单中?

时间:2015-08-05 05:20:22

标签: javascript angularjs forms

我有一个包含4个字段的表单

<form name="form">
    Name:
    <input type="text" ng-model="edit.name"><br />
     age:
    <input type="text" ng-model="edit.age"><br />
     phone:
    <input type="text" ng-model="edit.phone"><br />
     address:
    <input type="text" ng-model="edit.address"><br />

  </form>

我在app.js中有一个数组

 $scope.statuses = [
    {value: 1, text: 'status1'},
    {value: 2, text: 'status2'},
    {value: 3, text: 'status3'},
    {value: 4, text: 'status4'}
  ]; 

我在html中重复那个数组,我想要做的是我希望文本出现在表单中,例如&#34;编辑&#34;点击 PLUNKER

所以基本上当你点击我想要的编辑按钮时,你可以在plunker中看到&#34; status1&#34; &#34; STATUS2&#34; &#34; STATUS3&#34; &#34; STATUS4&#34;出现在#34; name&#34;形式的字段中&#34;年龄&#34; &#34;电话&#34;和&#34;地址&#34;

1 个答案:

答案 0 :(得分:0)

您的问题有点不清楚,但我认为您要做的是将状态更改为表单值。 我为你创建了这个plunker

你的ng-click功能。我将状态数组文本(在html中使用ng-repeat显示)更改为表单编辑值:

$scope.editFunction = function(edit){
   $scope.statuses[0].text=edit.name;
   $scope.statuses[1].text=edit.age;
   $scope.statuses[2].text=edit.phone;
   $scope.statuses[3].text=edit.address;
};

或者如果您希望数组值在表单中,只需在函数中反转= plunker: 但在这种情况下,如果您希望空表单起作用,则应在控制器之前声明编辑。

$scope.edit=[];

$scope.editFunction = function(edit){
   edit.name=$scope.statuses[0].text;
   edit.age=$scope.statuses[1].text;
   edit.phone=$scope.statuses[2].text;
   edit.address=$scope.statuses[3].text;
};

因此你正在使用ng-model.there不需要将参数传递给你的函数,你可以在 editFunction 中使用$ scope.edit而不是传递参数