2017-02-27 3 views
1

저는 초보자입니다. 두 개의 div 요소를 만들고 각도 js 프로그램을 실행하려고 할 때 몇 가지 각도 js 예제를 시도하고 있습니다. 초기화 된 배열이 두 번째 div에 인쇄되고 ng-repeat를 사용할 때 첫 번째 div에 대해 인쇄되지 않습니다. 아무도 그것이 왜 일어나고 있는지 말해 줄 수는 없어요. 나는 2 개의 배열이 초기화 된 방법 사이에 어떤 차이점을 볼 수 없다. 코드는 다음과 같습니다. 당신이 당신의 콘솔 첫 번째 ng-repeat에 중복에 대한 오류를 볼 수에 보면Angular js가 ng-repeat를 사용하여 ng-init의 데이터를 인쇄하지 않습니다.

<html data-ng-app> 
<head> 
<title>Angular js</title> 
</head> 
<body> 
<input type="text" data-ng-model="name">{{name}} 
<div class="container" data-ng-init="namess=['Hello','There','God','There']"> 
<h3>Looping with the ng-repeat Directive</h3> 
    <h4>Data to loop through is initialized using ng-init</h4> 
    <ul> 
     <li data-ng-repeat="Personname in namess">{{Personname}}</li> 
    </ul> 
</div> 
<div class="container" data-ng-init="names=['Dave','Napur','Heedy','Shriva']"> 
    <h3>Looping with the ng-repeat Directive</h3> 
    <h4>Data to loop through is initialized using ng-init</h4> 
    <ul> 
     <li data-ng-repeat="name in names">{{name}}</li> 
    </ul> 
</div> 
</body> 
<footer> 
<script type="text/javascript" src="angular.min.js"></script> 
</footer> 
</html> 

내가 얻고 그 결과

Looping with the ng-repeat Directive 

Data to loop through is initialized using ng-init 

Looping with the ng-repeat Directive 

Data to loop through is initialized using ng-init 

Dave 
Napur 
Heedy 
Shriva 
+0

. http://jsfiddle.net/cysa0cah/1/ – Nick

답변

3

다음과 같다. 배열에 "There"라는 문자가 두 번 있기 때문에 ng-repeattrack by을 추가해야합니다. 첫 번째 배열의 중복 항목이 있습니다

<li data-ng-repeat="Personname in namess track by $index">{{Personname}}</li> 
+0

내 문제를 해결해 주셔서 고맙습니다. –

1

와 콘솔을 선택하면 당신은 오류 "오류 : [ngRepeat 속는]을"볼 수 있습니다이로 교체하십시오. Angular ngRepeat는 중복 값을 허용하지 않습니다. https://docs.angularjs.org/error/ngRepeat/dupes에 대한 자세한 내용은 다음 링크를 확인하십시오.

가 수정 중 다음 중복 된 항목 (이) 나 사용을 제거하려면 : $ 지수에 의해 namess 트랙

<li data-ng-repeat="Personname in namess track by $index">{{Personname}}</li> 

데이터 NG 반복 = "PERSONNAME을,이 문제를 해결해야