2

For some reason, I have to use ng-repeat in order to display data. I'd like to display data without using ng-repeat.

angular:

App.controller('aboutLongCtrl', function ($scope, $http) {
    $http.get('test_data/ar_org.json')
    .then(function (res) {
        $scope.aboutlongs = res.data.aboutlong;
    });
});

Here's my markup:

<div class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
    <h6 ng-repeat="aboutlong in aboutlongs"><b>About {{aboutlong.name}}</b></h6>
    <div class="reason-content" ng-repeat="aboutlong in aboutlongs">
       {{aboutlong.description}}
    </div>
</div>

If I use this, it won't work.. can anyone explain why?

<div class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
    <h6><b>About {{aboutlong.name}}</b></h6>
    <div class="reason-content">
       {{aboutlong.description}}
    </div>
</div>

I followed this approach but it won't work: How to Display single object data with out Using ng-repeat?

I'm guessing it's because of my angular code, something isn't right.

2
  • 1
    without ng-repeat how to show the data's Commented Apr 10, 2017 at 5:59
  • Without ng-repeat u can do but create your own directive like ng-repeat. Commented Apr 10, 2017 at 6:46

3 Answers 3

2

Probably variable aboutlongs is an array of Objects.

In first case ng-repeat directive repeats a set of HTML, a given number of times.

If you still want to access the Object you can access the array Index and display it.

<div class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
        <h6><b>About {{aboutlongs[0].name}}</b></h6>
           <div class="reason-content">
           {{aboutlongs[0].description}}
          </div>
 </div>
Sign up to request clarification or add additional context in comments.

3 Comments

check above sample
I tried that but it won't work.. {{aboutlong[0].name}}
hi @Sajeetharan could you help stackoverflow.com/questions/43315615/…
1

ng-repeat is used to print an array

<div class="reason-content" ng-repeat="aboutlong in aboutlongs">
   {{aboutlong.description}}
 </div>

Here abvoutlongs is array and you are extracting single element in every repeat aboutlong..

<div class="reason-content"> {{aboutlongs[0].description}} {{aboutlongs[1].description}} </div>

this will also work .. as we are accessing element of array aboutlongs one by one

Comments

1

probably, the res.data.aboutlong is assigning an array to $scope.aboutlongs. And an array can only be displayed using ng-repeat. Alternatively, you may also use

<h6><b>About {{aboutlong[0].name}}</b></h6>

to avoid ng-repeat. However, I strongly suggest you to stick with ng-repeat.

3 Comments

Can you explain why I should stuck with ng-repeat when I don't need it?
Since that is an array that you are assigning to the variable and the very purpose of array is to display a list of similar repetitive objects. So, I suggested you to stick with the ng-repeat. It will help you handle when there is only one or a hundred objects coming in the array. Suppose, as of now, you are receiving two objects in the array and imagine that if you are using it as : {{aboutlong[0].name}} and {{aboutlong[1].name}} and going further you started to receive third object in the array, then you will have to come back and make changes in the code.
Thanks for the explanation, but in this control I won't need any repetition ever. I guess I shouldn't use the array to begin with. Thanks!

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.