2013-04-30 2 views
0

컨트롤러에서 ngRepeat 루프 내의 지정된 레코드에 액세스하려면 어떻게해야합니까? 궁극적으로 폼 유효성 검사를 위해 어떻게합니까?ngRepeat의 요소 액세스

I는 사용자가 제거 동적으로 추가 기록을 가질 수있는 다음과 같은 형식으로 /이 : 나는 직원 ID가 유효한지 확인해야 양식을 제출하기에

<table class="table table-condensed span12" id="tableParticipants"> 
    <!-- snip --> 
    <tbody> 
    <tr ng-repeat="person in people"> 
     <td><input type="text" pattern="[0-9]*" data-provide="typeahead" placeholder="8675309" ng-model="person.empid"></td> 
     <td><input type="text" data-provide="typeahead" placeholder="Firstname Lastname" ng-model="person.name"></td> 
     <td><input type="text" placeholder="Title" ng-model="person.title"></td> 
     <td style="text-align: center"><i class="icon-remove" ng-click="removeParticipant()"></i></td> 
    </tr> 
    </tbody> 
</table> 

을, 다른 나는 데이터베이스를 얻을 것이다 오류. 따라서 루프를 통해 작업 할 때 :

$scope.people.forEach(function(element, index, array) 
{ 
    if ($element['empid'] == BAD) 
    { 
    // set corredponding ngRepeat row to error 
    } 
} 

특정 레코드에 대해 해당 행에 어떻게 액세스합니까?

+0

왜 그렇게해야합니까? 당신은 오류 메시지와 함께 사람들의 배열을 채울 수 있으며, UI가 있다면 그것을 보여줄 수 있습니다 ... – lucuma

답변

2

다음과 같은 작업을 수행 할 수 있어야합니다. 배열 항목 및/또는 오류 메시지에 CSS 클래스 이름을 추가하십시오. 나머지는 업데이트 될 Angular로 처리해야합니다. 메시지 표시/숨기기, 수업 추가 등의 옵션이 있습니다.

<tr ng-repeat="person in people" ng-class="person.error"> 
<td><input type="text" pattern="[0-9]*" data-provide="typeahead" placeholder="8675309" ng-model="person.empid"></td> 
    <td><input type="text" data-provide="typeahead" placeholder="Firstname Lastname" ng-model="person.name"></td> 
    <td><input type="text" placeholder="Title" ng-model="person.title"></td> 
    <td style="text-align: center"><i class="icon-remove" ng-click="removeParticipant()"></i> <span>{{person.errorMessage}}</td> 
</tr> 

if ($element['empid'] == BAD) 
{ 
    $element['errorMessage'] = 'Invalid Id'; // could bind or show a span/depends. 
    $element['error'] = 'error'; // set to whatever class you want to use. 
} 
+0

빙고. 그것이 제가 물어 보는 것입니다. 원형처럼 보일 것입니다. AngularJS의 정신을 유지하기 위해 DOM 요소에 직접 액세스해서는 안되는 것을 알고, 충분히 표현하지 못했습니다. 나는 지금 빨간 줄이 갑자기 나타나고있다. 고맙습니다! –

+0

일을하는 jQuery 방식에서 전환하는 것은 처음에는 힘들 수 있습니다. – lucuma