2016-07-21 2 views
1

다른 사용자가 입력 필드를 사용하여 의견을 남길 수있는 작업 목록을 반복합니다. ng-repeat를 사용하여 목록을 반복하고 ng-model 및 범위를 사용하여 메모 입력 값을 서버에 보내려고합니다. 콘솔 로깅을 테스트 중이지만 정의되지 않은 것으로 표시됩니다. 도와주세요!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}`); 
    }; 

이 내가 당신이 얻고있는

답변

1

NG 반복과 디스플레이 항목을 이용하여보다 더 많은 일을 시도한 것은 이번이 처음이다 undefined은 을 생성하므로 ngRepeat이 생성됩니다.

ngModel은 항상 Dot Rule 또는 controller-as-syntax을 사용하십시오.

하면 컨트롤러에 넣어 :

<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}`); 
}; 

참고 :

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

그럼 당신이 뭔가를 할 수 있습니다 :

$scope.model = {}; 

다음이로 ngModel를 사용 당신의 함수는 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> 

자바 스크립트, @ developer033에서 도움을

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

감사합니다 :로 변경 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}`); 
    };