2016-07-21 8 views
1

他のユーザーが入力フィールドを使用してコメントできるようにする作業のリストを繰り返しています。 ng-repeatでリストを繰り返し、ng-modelとscopeでコメント入力の値をサーバに送信しようとしています。私はコンソールログでテストしていますが、未定義と表示されます。助けてください!ng-repeat内の入力ngモデル

HTML:

<div class="taskContainer"> 
    <div ng-repeat='task in taskList' class="task"> 
    <div class="postedBy"> 
     <h6>{{task.user.userName}}</h6> 
    </div> 
    <h4>{{task.taskText}}</h4> 
    <div class="comments"> 
     <input ng-model="newComment" type="text" placeholder="comments"> 
     <button ng-click='comment(task.taskId)' type="button" name="button">Add</button> 
     <h6>{{task.commentText}}</h6> 
    </div> 
    </div> 
</div> 

JSコントローラ:

$scope.comment = function(id,text){ 
     console.log(`send comment text ${$scope.newComment}`); 
     console.log(`task Id: ${id}`); 
    }; 

これは、私はあなたが取得しているNGリピート

答えて

1

で表示itmes以上のことをしようとしたのは初めてですundefinedngRepeatが作成するためです。

ngModelは、常にDot Ruleまたはcontroller-as-syntaxを使用して割り当てます。

あなたのコントローラでそれを入れて:

$scope.model = {}; 

次に、このようngModelを使用します。

<input ng-model="model.newComment[$index]" type="text" placeholder="comments"> 

その後、あなたはこのような何か持つことができます。

<div class="taskContainer"> 
    <div ng-repeat="task in taskList track by $index" class="task"> 
    <div class="postedBy"> 
     <h6>{{task.user.userName}}</h6> 
    </div> 
    <h4>{{task.taskText}}</h4> 
    <div class="comments"> 
     <input ng-model="model.newComment[$index]" type="text" placeholder="comments"> 
     <button ng-click='comment($index)' type="button" name="button">Add</button> 
     <h6>{{task.commentText}}</h6> 
    </div> 
    </div> 
</div> 

$scope.comment = function(index) { 
    console.log(`send comment text ${$scope.model.newComment[index]}`); 
    console.log(`task Id: ${taskList[index].id}`); 
}; 

注:をあなたの関数は2つのパラメータを期待しています。

$scope.comment = function(id) { 
+0

に役立ちますが、それは繰り返しの項目に固有のものではない、それは変わる意味しますすべての仕事のフィールド –

+0

私は理解していない..あなたが言ったことは? – developer033

+0

私は –

-1

HTML、

<input ng-model="newComment" type="text" placeholder="comments"> 
<button ng-click='comment(task.taskId, newComment)' type="button" name="button">Add</button> 

のJavaScript、

$scope.comment = function(id, text) { 
    console.log(`task Id: ${id}`); 
    console.log(`send comment text ${text}`); 
}; 
0

developer033 @から助けてくれてありがとうを:に変更しULD。ここ は私の問題を解決するものである:

HTML:

<div class="taskContainer"> 
    <div ng-repeat="task in taskList track by $index" class="task"> 
    <div class="postedBy"> 
     <h6>{{task.user.userName}}</h6> 
    </div> 
    <h4>{{task.taskText}}</h4> 
    <div class="comments"> 
     <input ng-model="model.newComment[$index]" type="text" placeholder="comments"> 
     <button ng-click='comment(task.taskId,$index)' type="button" name="button">Add</button> 
     <h6>{{task.commentText}}</h6> 
    </div> 
    </div> 
</div> 

とJS:

$scope.model = {}; 
    $scope.comment = function(id, index) { 
     console.log(`send comment text ${$scope.model.newComment[index]}`); 
     console.log(`task Id: ${id}`); 
    }; 
関連する問題