Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display single object data with out Using ng-repeat?

Tags:

angularjs

Aim :- To display only Single Object data. Do I have to use ng-repeat to get the object ?

I'm relatively new to angular. Is there a better way to do this?

Html View :-

<div ng-controller="dashboardController">
    <div ng-repeat="person in persons">
        <span class="name">{{person.name}}</span>
        <span class="title">{{person.title}}</span>
    </div>
</div>

Controller Code :-

.controller('dashboardController', ['$scope', function(scope){
    scope.persons = [
      {
        name:"George Harrison",
        title:"Goof Off extraordinaire"
      }
    ];
}])

UPDATE FOR MY FELLOW NOOBS, array vs single data set:

scope.persons = [ <-- that creates an array. Cant believe I forgot that.
scope.persons = { <-- that creates a single data set
like image 316
Acts7Seven Avatar asked Feb 07 '23 14:02

Acts7Seven


1 Answers

scope.persons is an array so you have to use ng-repeat. if you your data is an object, you don't need to use ng-repeat. ex: your controller

    .controller('dashboardController', ['$scope', function(scope){
    scope.person ={
        name:"George Harrison",
        title:"Goof Off extraordinaire"
      }

}]);

so your html:

<div ng-controller="dashboardController">
<div>
    <span class="name">{{person.name}}</span>
    <span class="title">{{person.title}}</span>
</div>

like image 86
Van Nguyen Avatar answered Feb 13 '23 05:02

Van Nguyen