2017-01-18 4 views
7

は私Plunkerある `ngRepeat'edからのデータを印刷することはできません:https://plnkr.co/edit/rBGQyOpi9lS0QtnCUq4Lはここtextarea`

私は、各textareaタグで入力されているものconsole.log()にしたいです。

angular.js:14290 TypeError: Cannot read property 'data' of undefined 
    at b.$scope.printStuff (index.html:31) 
    at fn (eval at compile (angular.js:15118), <anonymous>:4:299) 
    at b.$eval (angular.js:17922) 
    at angular.js:25653 
    at Object.<anonymous> (angular.js:28429) 
    at q (angular.js:325) 
    at Object.$$writeModelToScope (angular.js:28427) 
    at angular.js:28420 
    at g (angular.js:28339) 
    at f (angular.js:28322) 

がどのように私はこのエラーを修正します:

 $scope.printStuff= function(customize,item){ 
      console.log(customize[item.index].data); 
     }; 

私はどのtextareaに入力を開始、私はこのエラーを取得:textareaでのタイピングがprintStuff()機能をトリガ?私はまだエラーを取得MannFromRenoのANSWER

で更新

。ここに私のPlunkerはあります:

+0

に、あなたはprintStuffにselected.index(番号0)を送信している、とprintStuffの最後の引数はオブジェクトでありますプロパティインデックスを持つ "item" – progysm

+0

@progysmどうすればこの問題を解決できますか?私はまだAngularにはかなり新しいので、何をすべきかはわかりません... – Username

答えて

5

あなたはindexプロパティを取得しますか? $indexng-repeatで提供)を使用できます。

が更新plunkerを参照してください:https://plnkr.co/edit/rOTUoLDWX195Uh0JBXwj

はこれが何をしたいの動作です、私が正しいのですか?

2

あなたが存在していなかったitem.index財産への結合であり、作っていたミスの

<textarea rows="5" cols="50" placeholder="Paste data here." ng-model="customize[$index].data" ng-change="printStuff($index)"></textarea> 

とあなたのprintStuff機能

$scope.printStuff = function(index) { 
    console.log($scope.customize[index].data); 
}; 

ような結合あなたのテキストエリアを更新します。 ng-repeatスコープ内で$ indexを使用すると、現在の反復インデックスを取得できます。

は、あなたが以下試すことができ、このplnkr

2

を参照してください。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script type="text/javascript"> 
 
\t \t var app= \t angular.module("myApp",[]); 
 

 
\t \t app.controller("myCTRL",function($scope){ 
 
\t \t \t $scope.items= \t [{}]; 
 

 
\t \t \t $scope.customize= \t [{data: 'hi'}]; 
 

 
\t \t \t $scope.addChart= \t function(){ 
 
\t \t \t \t $scope.customize.push({ 
 
\t \t \t \t \t data: '' 
 
\t \t \t \t }); 
 
\t \t \t }; 
 

 
\t \t \t $scope.removeChart= \t function(){ 
 
\t \t \t \t $scope.items.splice(-1,1); 
 
\t \t \t }; 
 
$scope.removeWhichone = function(id, data){ 
 
    //$scope.customize.splice($scope.customize.indexOf(data), 1); 
 
    angular.forEach($scope.customize, function(value, key) { 
 
      if (key === id){ 
 
       $scope.customize.splice(key, 1); 
 
      } 
 
     }); 
 
} 
 

 
\t \t \t $scope.printStuff= \t function(customize,index){ 
 
\t \t \t // \t console.log(customize[index].data); 
 
\t \t \t }; 
 
\t \t }); 
 
\t </script> 
 

 
<body ng-app="myApp" ng-controller="myCTRL"> 
 
\t <div> 
 
\t \t <button ng-click="addChart()">Add</button> 
 
\t \t <!--<button ng-click="removeChart()">Remove</button> --> 
 
     
 
\t </div><br> 
 
\t <div ng-repeat="item in customize"> 
 
\t \t <form novalidate> 
 
\t \t \t <textarea rows="5" cols="50" placeholder="Paste data here." ng-model="customize[$index].data" ng-change="printStuff(customize,$index)"></textarea> 
 
\t \t \t <button ng-click="removeWhichone($index, item)">Remove</button> 
 
\t \t \t <br><br> 
 
\t \t </form> 
 
\t \t 
 
\t </div> 
 
\t {{customize}} 
 
</body>

1

アップデートそれに:

<div ng-repeat="item in items" ng-init="index = $index;"> 
    <form novalidate> 
     <textarea rows="5" cols="50" placeholder="Paste data here." ng-model="customize[index].data" ng-change="printStuff(customize, selected.index)"></textarea> 
     <br> 
    </form> 
</div> 

ngModel

はここで作業PlunkerだとしてNG-モデルはオブジェクト自体カスタマイズオブジェクト内のデータプロパティこととはなりません:https://plnkr.co/edit/lqA5Fg8UAz81IM9v3Kts?p=preview

+0

あなたの回答と私が数日前に受け入れた回答との違いは何ですか?説明できますか? – Username

+0

ああ、私は受け入れられた答えを本当にチェックしていない、私はちょうどあなたがまだエラーがあったと答え、ソリューションを掲載したと言った。 –

1
UIから

パス$indexは、パラメータとしてprintStuff機能最新pluckerで

<form novalidate> 
    <textarea rows="5" cols="50" placeholder="Paste data here." ng- model="$scope.customize[$index]" ng-change="printStuff(customize,item,$index)"></textarea> 
    <br> 
</form> 

$scope.printStuff= function(customize,item,index){ 
    console.log(customize[index].data); 
}; 

https://plnkr.co/edit/rBGQyOpi9lS0QtnCUq4L?p=preview