R K
R K

Reputation: 327

Not able to loop values in angularjs

I have a code and I want to loop throught the array in json format by using ng-repeat and ng-init.

But the code isn't working.

Below is the following code.


<!DOCTYPE html>
<html ng-app="">
<head>
    <title></title>
</head>
<body>
    <h1>
        ng - repeat
    </h1>
    <hr />
    <div ng-init="myFavLan=[{name:'PHP',extension:'.php'},
                            {name:'Javascript',extension:'.js'},
                            {name:'HTML',extension:'.html'}
                           ]">
        <p ng-repeat="language in myFavlan">
            Name : {{ language.name }} <br />
            Extension : {{ language.extension }}
        </p>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>
</html>

Please note that I am a beginner in angularjs

Upvotes: 2

Views: 51

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222582

It should be myFavLan Change

From

<p ng-repeat="language in myFavlan">

To

<p ng-repeat="language in myFavLan">

DEMO

<!DOCTYPE html>
<html ng-app="">
<head>
    <title></title>
</head>
<body>
    <h1>
        ng - repeat
    </h1>
    <hr />
    <div ng-init="myFavLan=[{name:'PHP',extension:'.php'},
                            {name:'Javascript',extension:'.js'},
                            {name:'HTML',extension:'.html'}
                           ]">
        <p ng-repeat="language in myFavLan">
            Name : {{ language.name }} <br />
            Extension : {{ language.extension }}
        </p>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>

Upvotes: 2

CShark
CShark

Reputation: 1622

Javascript is case-sensitive, so the variable myFavlan you have in the ng-repeat is undefined. Just change

<p ng-repeat="language in myFavlan">

to

<p ng-repeat="language in myFavLan">

You could also change your ng-init variable.

Upvotes: 3

Related Questions