2015-06-05 19 views
2

我正在動態添加輸入,但是一旦我嘗試在元素中添加一個輸入,就是在由於ng-repeat導致的第二手元素中添加相同的輸入:動態輸入輸入,避免重複在不同的元素上

<div ng-repeat="operation in operations"> 

      <p>{{operation.hola}}</p> 

      <button class="btn" ng-click="addNewChoice([])">Add fields</button>     

      <div data-ng-repeat="choice in choices track by $index"> 
       <input type="text" ng-model="choice.text">      
      </div>    

    </div> 

控制器

$scope.choices = []; 
$scope.operations = [{ 
     hola: 'HELLO' 
    }, { 
     hola: 'BYE' 
    } 
    ]; 

$scope.addNewChoice = function() { 
    $scope.choices.push(['']); 
}; 

當你點擊Add Fields按鈕,應該將只有一個輸入正確的箱/形式,而不是在這兩個盒子。

我沒有很好地解釋我自己,但在這裏我有一個JSBin所以你可以檢查我的問題。

+0

你必須停止編輯該jsbin所以別人可以看看:) – sirrocco

+0

@sirrocco haha​​haha是的,對不起。我只是停下來。 – NietzscheProgrammer

+0

將選擇數組添加到操作數組對象...否則它是一個常用數組,它不會工作 – sirrocco

回答

1

爲什麼你不能修改json?現在在你的程序中 - 你可以做任何事情。

以下是我會做:

$scope.operations = [{hola: 'HELLO'}, {hola: 'BYE'}]; 

// this is JS - you have the power :) 
// call this right after you get the json from your ajax call or something 
$scope.operations = preProcessOperations($scope.operations);  


$scope.addNewChoice = function(choices) { 
    choices.push(['']); 
}; 

function preProcessOperations(ops){ 
    for(var i = 0; i < ops.length; i++){ 
    ops[i].choices = []; 
    } 
} 

HTML:

<div ng-repeat="operation in operations"> 
    <p>{{operation.hola}}</p> 

    <button class="btn" ng-click="addNewChoice(operation.choices)">Add fields</button>  

    <div data-ng-repeat="choice in operation.choices track by $index"> 
     <input type="text" ng-model="choice.text">      
    </div> 
</div> 
+0

我的第一個ng-repeat從數據操作開始。操作''',我仍然在嘗試。 – NietzscheProgrammer

+0

我不完全確定你的意思。你如何獲得你的數據? – sirrocco

1

如果您希望choices對於每個操作都不相同,則需要創建2個不同的choices陣列。最好的辦法是建立在每個操作chioces對象:

$scope.operations = [{ 
    hola: 'HELLO', 
    choices: [] 
}, { 
    hola: 'BYE', 
    choices: [] 
}]; 

然後在你的HTML:

<div ng-repeat="operation in operations"> 
    <p>{{operation.hola}}</p> 

    <button class="btn" ng-click="operation.choices.push([])">Add fields</button>  

    <div data-ng-repeat="choice in operation.choices track by $index"> 
     <input type="text" ng-model="choice.text">      
    </div> 
</div> 

編輯:,如果你不希望修改操作陣列出於某種原因,您可以爲選擇創建一個2維數組襤褸:

$scope.operationChoices = []; 
for (var i = 0; i < operations.length; ++i) { 
    $scope.operationChoices.push([]); 
} 

那麼你的HTML是:

<div ng-repeat="operation in operations" ng-init="choices = operationChoices[$index]"> 
    <p>{{operation.hola}}</p> 

    <button class="btn" ng-click="choices.push([])">Add fields</button>  

    <div data-ng-repeat="choice in choices track by $index"> 
     <input type="text" ng-model="choice.text">      
    </div> 
</div> 
+0

nope,我無法添加'''選項:[]'''在這裏我的朋友。你必須考慮到我收到了一個我無法修改的json,所以添加這個空數組的技術這次不會工作。 – NietzscheProgrammer

+0

@NietzscheProgrammer:你爲什麼不能在收到它後修改JSON? –

+0

我的第一個ng-repeat是以'''data.operations''中的操作開始的,我還在掙扎。 – NietzscheProgrammer

2

略有不同的方式,你可以試試這個太

<div ng-repeat="operation in operations">    
     <div class="panel">    
      <div class="panel-body">     
       <p>{{operation.hola}}</p>     
      <button class="btn" ng-click="addNewChoice(operation)">Add fields</button> 
      <div data-ng-repeat="choice in choices[operation.hola] track by $index"> 
       <input type="text" ng-model="choice.text">      
      </div> 
      </div>     
     </div>   
    </div> 

JS

angular.module('ionicApp',[]) 

.controller('mainCtrl', function($scope) { 
    $scope.choices = {}; 
    $scope.operations = [{ 
      hola: 'HELLO' 
     }, { 
      hola: 'BYE' 
     }]; 

    $scope.addNewChoice = function(operation) { 
     $scope.choices[operation.hola] = $scope.choices[operation.hola] || []; 
     $scope.choices[operation.hola].push(['']); 
    };  
}); 

DEMO