2

I am new to angularjs currently I am learning how to read array objects and display in view.

HTML Code:

<body ng-app="myDirective" ng-controller="myController">
<div class="container border_bottom">
    {{welcomeMessage}}<br />
    <label>First Name</label> : {{fullname.firstName}}<br />
    <label>Last Name</label> : {{fullname.LastName}}<br />
    Employee Details <br /> 
    <ul>
        <li ng-repeat="emp in employee">
            <div>                    
                <label>Employee ID</label>{{employee.empId}}<br/>
                <label>Employee Name</label>{{employee.empName}}<br/>
                <label>Voter Count</label>{{employee.voteCount}}
            </div>
        </li>
    </ul>

</div>

AngularJS Code:

var myApp = angular.module("myDirective", []);

myApp.controller("myController", function ($scope) {
$scope.welcomeMessage = "Hello ";
$scope.fullname = { firstName: "Mahadevan", LastName: "Sivasubramanian" }

$scope.employee = [
    { empId: 1, empName: "Mahadevan", voteCount: 0 },
    { empId: 2, empName: "john", voteCount: 0 },
    { empId: 3, empName: "Siva", voteCount: 0 }
];
});

I am not getting any error. Where I am doing mistake?

3
  • it works fine, what is the issue? Commented Aug 13, 2017 at 3:21
  • I am not getting the value of array Commented Aug 13, 2017 at 3:24
  • check the answer below Commented Aug 13, 2017 at 3:24

2 Answers 2

1

You should access the current element by emp in the model of your ng-repeat

<li ng-repeat="emp in employee">
            <div>                    
                <label>Employee ID</label>{{emp.empId}}<br/>
                <label>Employee Name</label>{{emp.empName}}<br/>
                <label>Voter Count</label>{{emp.voteCount}}
            </div>
</li>

ng-repeat simple usage

<div ng-repeat="anElement in anArray">
  {{anElement}}
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

I hope SO questions are designed to be answered more than once if one deems that is appropriate.
It should be - "Yes you are correct, but not the same *words"
Well if it so, then my mistake. Usually its the answers should not be the same! btw I did not downvote
Well I never thought you did. But it is easier to find in SO the answers which cite same solution for a problem than different ones.
0

You should access emp not employee inside ng-repeat

<li ng-repeat="emp in employee">
                <div>                    
                    <label>Employee ID</label>{{emp.empId}}<br/>
                    <label>Employee Name</label>{{emp.empName}}<br/>
                    <label>Voter Count</label>{{emp.voteCount}}
                </div>
</li>

DEMO

var myApp = angular.module("myDirective", []);
myApp.controller("myController", function ($scope) {
$scope.welcomeMessage = "Hello ";
$scope.fullname = { firstName: "Mahadevan", LastName: "Sivasubramanian" }
$scope.employee = [
    { empId: 1, empName: "Mahadevan", voteCount: 0 },
    { empId: 2, empName: "john", voteCount: 0 },
    { empId: 3, empName: "Siva", voteCount: 0 }
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myDirective" ng-controller="myController">
<div class="container border_bottom">
    {{welcomeMessage}}<br />
    <label>First Name</label> : {{fullname.firstName}}<br />
    <label>Last Name</label> : {{fullname.LastName}}<br />
    Employee Details <br /> 
    <ul>
        <li ng-repeat="emp in employee">
            <div>                    
                <label>Employee ID</label>{{emp.empId}}<br/>
                <label>Employee Name</label>{{emp.empName}}<br/>
                <label>Voter Count</label>{{emp.voteCount}}
            </div>
        </li>
    </ul>

</div>

2 Comments

actually my trainer accessing emp not employee why for them is coming but not for me
you should access emp only

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.