2016-08-13 11 views
0

角度を使用して必要な数の入力を作成できるようにしていますが、入力の値を表示する簡単なアラートが必要です。私はリピートを使用していない場合は値を取得できましたが、リピートを行った後は値を取得できました。角度リピータを使用した複数の入力のアラート入力値

<div ng-controller="MainCtrl"> 
    <form id="quickPick-form1" class="list" ng-submit="submit()" > 
     <fieldset data-ng-repeat="choice in choices" class="clearfix"> 
      <input type="text" placeholder="Item" class="pull-left" ng-model="item"> 
      <button class="remove pull-left" ng-show="$last" ng-click="removeChoice()">X</button> 
     </fieldset> 
     <button id="quickPick-button1" style="font-weight:600;font-style:italic;" class="addfields button button-calm button-block button-outline " ng-click="addNewChoice()">Tap me to add an item!</button> 
     <div class="spacer" style="width: 300px; height: 40px;"></div> 
     <p>Have enough items added? Click Randomize</p> 
     <button ng-click="showChoice()" id="quickPick-button2" class=" button button-calm button-block ">Randomize</button> 
     <div class="spacer" style="width: 300px; height: 20px;"></div> 
    </form> 
</div> 
<script> 
.controller('MainCtrl', function($scope) { 
    $scope.choices = [{id: 'choice1'}, {id: 'choice2'}]; 

    $scope.addNewChoice = function() { 
    var newItemNo = $scope.choices.length+1; 
    $scope.choices.push({'id':'choice'+newItemNo}); 
    }; 

    $scope.removeChoice = function() { 
    var lastItem = $scope.choices.length-1; 
    $scope.choices.splice(lastItem); 
    }; 

    $scope.item = null; 
    $scope.showChoice = function(){ 
    alert($scope.item); 
    }; 

}); 
</script> 

答えて

0

この

$scope.showChoice = function(){ 
    alert($scope.choices.split(" /n")); 
}; 

itemがあなたのスコープ内ではありません試してみてください。範囲内にはchoices[]しかありません。

+0

おっとに見えますが、それは[NG-モデル= "項目"]です。私は例を更新しました – Rhino

0

ここにはPlunkerがあります。モデルをchoice.itemに変更する必要があります。

 <form id="quickPick-form1" class="list" ng-submit="submit()" > 
    <fieldset data-ng-repeat="choice in choices" class="clearfix"> 
     <input type="text" placeholder="Item" class="pull-left" ng-model="choice.item"> 
     <button class="remove pull-left" ng-show="$last" ng-click="removeChoice()">X</button> 
    </fieldset> 
    <button id="quickPick-button1" style="font-weight:600;font-style:italic;" class="addfields button button-calm button-block button-outline " ng-click="addNewChoice()">Tap me to add an item!</button> 
    <div class="spacer" style="width: 300px; height: 40px;"></div> 
    <p>Have enough items added? Click Randomize</p> 
    <button ng-click="showChoice()" id="quickPick-button2" class=" button button-calm button-block ">Randomize</button> 
    <div class="spacer" style="width: 300px; height: 20px;"></div> 

とあなたのJavaScriptで:私は一例でアイテムを残しよう

 $scope.showChoice = function(){ 
    var myChoices = ''; 
    for(var i =0; i < $scope.choices.length; i++){ 
     myChoices = myChoices + ', ' + $scope.choices[i].item; 
    } 
    alert(myChoices) 
    }; 
関連する問題