1

i've tried the last two days on a little problem.

I'm new on AngularJS, and I don't understand why this is not working.

If there is a same value in an array, the nested ng-repeat doesn't work :(

Is someone can help me please ?

index.html :

<!DOCTYPE html>
<html ng-app="testApp">
<head>
    <title></title>
</head>
<body ng-controller="testCtrl">
    <ul>
        <li ng-repeat="item in items">
            {{item.ip}}
            <ul>
                <li ng-repeat="s in item.status">{{s}}</li>
            </ul>
        </li>
    </ul>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script src="app.js"></script>
</body>
</html>

app.js (this is working)

var app = angular.module('testApp', []);

app.controller('testCtrl', function ($scope) {

$scope.items = [
    {ip: '192.168.1.23', status: ['aaa','bbb','ccc','ddd','eee','fff']},
    {ip: '192.168.1.24', status: ['ggg','hhh','iii','jjj','kkk','lll']}
];

});

app.js (doesn't work)

var app = angular.module('testApp', []);

app.controller('testCtrl', function ($scope) {

$scope.items = [
    {ip: '192.168.1.23', status: ['na','up','na','down','down','na']},
    {ip: '192.168.1.24', status: ['up','down','na','up','up','na']}
];

});

Thanks a lot

2 Answers 2

2

You have to use track by expression to tell angular that elements are different. Try

<ul>
    <li ng-repeat="item in items">
        {{item.ip}}
        <ul>
            <li ng-repeat="s in item.status track by $index">{{s}}</li>
        </ul>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

where there are same values append "track by $index" to ng-repeat like

  ng-repeat="s in item.status track by $index"

2 Comments

I can't at this time sorry, i've only 2pts and I need 15 to vote :(
@StephaneTanguy now you can, see :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.