2016-04-28 162 views
0

我想創建動態表單字段後點擊添加行按鈕。動態創建添加表單點擊添加按鈕在angularjs

<form name="form" class="form-validation form-inline" > 
<div class="form-group"> 

    {!! 
    Form::text('name',null,['class'=>'form-control','placeholder'=>'Name','data-ng-model'=>'name','required'=>'required']) 
    !!} 

    {!! 
    Form::text('description',null,['class'=>'form-control','placeholder'=>'Description','data-ng-model'=>'description']) 
    !!} 

</div> 
<div class="form-group"> 
    <button class="btn btn-default " ng-disabled="form.$invalid" 
      data-ng-click="addTag()"> 
     Add 
    </button> 
</div> 


添加行

<div ng-repeat="input in form"> 
           <form name="form" class="form-validation form-inline"> 
            <div class="form-group"> 

             {!! 
             Form::text('name',null,['class'=>'form-control','placeholder'=>'Name','data-ng-model'=>'input.name','required'=>'required']) 
             !!} 

             {!! 
             Form::text('description',null,['class'=>'form-control','placeholder'=>'Description','data-ng-model'=>'input.description']) 
             !!} 

            </div> 
            <div class="form-group"> 
             <button class="btn btn-default " ng-disabled="form.$invalid" 
               data-ng-click="addTag()"> 
              Add 
             </button> 
            </div> 

           </form> 
            </div> 

而且控制部分等之後編輯。 我應該如何將它們添加到數據庫中? Angularjs控制器:

$scope.addTag = function() { 
    var tag = { 
     name: $scope.name, 
     description: $scope.description 
    }; 

    $http.post('tags', tag) 
     .success(function (data) { 
      console.log('www.sabin.info.np'); 
      $scope.tags = data; 
      $scope.name = ''; 
      $scope.description = ''; 
     }).error(function (err) { 
     console.log('error'); 
    }); 
}; 

和代碼,我已編輯的是:

$scope.form = []; 
$scope.addRow = function() { 
    $scope.form.push({ 
     name: '', 
     description: '' 
    }); 
} 

add row field

+2

看到這個答案http://stackoverflow.com/questions/ 32470928/angular-formly-adding-form-fields-dynamic-on-user-click/35603088#35603088 –

回答

0

首先,需要通過指定對象的陣列初始化輸入形式的範圍。例如:

$scope.form = []; 

之後,只要這個功能添加到您的控制器:

$scope.addRow = function() { 
    $scope.form.push({ 
     name: '', 
     description: '' 
    }); 
} 

而使用NG-重複迭代形式

<div ng-repeat="input in form"> 
    <input ng-model="input.name" /> 
    <input ng-model="input.description" /> 
</div> 

[增訂]

所以,我想你想要將每行輸入的數據發送到端點爲的後端。方法如下:

實際上所有的表單數據都存儲在$scope.form中,它是每個對象內部具有namedescription屬性的對象數組。所以如果你想訪問第一行的名稱/描述值,你可以通過$scope.form[0].name$scope.form[0].description訪問該值。

所以發送數據的正確方法是:

在HTML:

<div ng-repeat="input in form"> 
    <input ng-model="input.name" /> 
    <input ng-model="input.description" /> 
    //You pass the input object throught the function params 
    <button ng-click="addTag(input)">Add Tag</button> 
</div> 

在控制器:

$scope.addTag = function (input) { 
    $http.post('tags', input) 
     .success(function (data) { 
      console.log('www.sabin.info.np'); 
     }).error(function (err) { 
     console.log('error'); 
    }); 
}; 
+0

非常感謝你,我想問另一個問題,我面臨的問題 – user2851129

+0

這是發佈到數據庫,如何將數據推送到數據庫 $ scope.addTag = function(){ var tag = {0}名稱:$ scope.input.name, 描述:$ scope.input.description }; $ http.post( '標籤',標籤) .success(功能(數據){ 的console.log( 'www.sabin.info.np'); $ scope.tags =數據; $範圍。 input.name =''; $ scope.input.description =';; })。error(function(err){0}日誌( '錯誤'); }); }; – user2851129

+0

對不起,您能否爲此更新您的問題?我只是不明白你在問什麼。 –

相關問題