javascript
  • angularjs
  • angularjs-directive
  • angularjs-scope
  • 2016-11-22 61 views -2 likes 
    -2

    我添加了一些新行,但我還想在數據庫中更新新的行數據,但沒有更新的數據如何獲得新的行數據還添加地址JSON數組如何在當前的JSON數組添加新行數據angularjs

     <tr ng-repeat="x in Profile.addresses"> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td> 
    

    <tr ng-repeat="lines in array"> 
    
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.zip_code ' name='zip_code'></td> 
        <td><input type="text" class="form-control" id="inputDefault" ng-model='x.phone_number ' name='phone_number'></td> 
    
         </tr> 
    

    <div class="col-md-2"> 
    
    <a href="" data-toggle="tooltip" title="Add Address" ng-click="addRow()"><i class="fa fa-plus fa-2x cust_primary" aria-hidden="true"> 
    

    enter image description here

    +0

    你可以使用mvc的優勢而不是嘗試做dom操作 –

    回答

    1

    在你的控制器中,創建一個計數器和一個數組。

    $scope.i = 0; 
    $scope.array = []; 
    

    每當用戶點擊按鈕,添加一個到櫃檯和創建陣列。

    $scope.addRow = function() { 
        $scope.i++; 
        $scope.array = []; 
        for(var i = 0; i < $scope.i; i++) { 
         $scope.array.push(i); 
        } 
    } 
    

    在你的html中,只需重複基於該計數器的行。

    <tr ng-repeat="lines in array"> 
        // Your tds 
    </tr> 
    

    編輯因爲我無法理解你的英語,我會給你一個通用的答案。

    你的控制器必須有對象

    $scope.i = 0; 
    $scope.array = [{ 
        id: 0, 
        address: 'address 1', 
        name: 'Jack Reacher' 
    }, { 
        id: 1, 
        address: 'address 2', 
        name: 'Ethan Hawk' 
    }]; 
    

    的數組,那麼你將略有改變你的addRow功能。

    $scope.addRow = function() { 
        $scope.i++; 
        $scope.array = []; 
        for(var i = 0; i < $scope.i; i++) { 
         $scope.array.push({ 
          id: null, 
          address: '', 
          name: '' 
        }); 
        } 
    } 
    

    然後在你的HTML中使用它。

    <tr ng-repeat="object in array"> 
        <td>{{object.id}}</td> 
        <td><input type="text" ng-model="object.address" /></td> 
        <td><input type="text" ng-model="object.name" /></td> 
    </tr> 
    

    如果你想保存它,您使用的$http請求。我會讓你處理那部分。

    +0

    其工作感謝bro –

    +0

    如何添加這些新的添加行值,但當前的地址地址數組? –

    +0

    對不起,你能改說嗎? – trichetriche

    相關問題