0

Dies ist mein Controller:Angular - Dynamische ng-Modellname mit der ID in ng-repeat

$scope.inputs = []; 
$scope.addInput = function() { 
    $scope.inputs.push({ 
     id: $scope.inputs.length + 1, 
    }); 

}; 
$scope.removeInput = function (index) { 
    $scope.inputs.splice(index, 1); 
}; 

ich auf diese Weise versuchen, aber hat nicht funktioniert:

<div ng-repeat="input in inputs"> 
     <textarea id="activity" 
        name="project_budget_administration[<% input.id %>][activity]" 
        placeholder="Activity" 
        ng-model="project_budget_administration.activity_<% input.id %>" 
        ng-maxlength="100" 
        required></textarea> 
</div> 

Für Namensattribut von TextArea- funktioniert perfekt, aber für ng-model - nicht. Auch ich versuchte mit dieser Syntax ng-model = "project_budget_administration.activity [input.id]" und anstelle von input.id versuchte ich $ index, auch nicht funktioniert.

+0

Ihre Syntax ist nicht sinnvoll. '<% %>' sind keine gültigen eckigen Tags, und ich sehe nicht, warum '$ index' oder' item' funktionieren würden - wie sie nicht definiert sind? –

+0

können Sie festlegen, welche Tags Sie wollen, also wähle ich "<% %>" –

+0

kann Ihnen helfen http://StackOverflow.com/Questions/32470928/angular-formly-Adding-form-fields-dynamically-on-user-click/35603088 # 35603088 –

Antwort

0

können Sie JavaScript Ausdrücke verwenden, um dynamisch die ng-Modell zu nennen

<div ng-repeat="input in inputs"> 
    <textarea id="activity" 
       name="project_budget_administration[<% input.id %>][activity]" 
       placeholder="Activity" 
       ng-model="project_budget_administration['activity_' + input.id]" 
       ng-maxlength="100" 
       required></textarea> 

Fiddle aus einer anderen Antwort - http://jsfiddle.net/DrQ77/-How can I set a dynamic model name in AngularJS?

+0

kein Mann, hat nicht funktioniert, wenn ich Element inspiziere es ist zeigt wie Klartext: " ng-model = "project_budget_administration ['activity_' + input.id]" –

+0

Wen sehen Sie, wenn Sie '{{project_budget_administration}} loggen, dann sollten Sie in diesem Modell ein Feld von activity_1 sehen (abhängig von der ID) mit dem Daten, die Sie in das Textfeld eingeben – KieranDotCo

+0

"project_budget_administration" Dies ist nur ein zufälliger Name, den ich mache –