0

I have an array of doubles (they turn into doubles eventually) in my controller that I am trying to print on my web page using ng-repeat, but I am unsure how to do so. The array is simply":

vm.calorieArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

where vm is the alias of my controller.

In my html I have a div that looks like this that is attempting to print it out:

<div ng-repeat="value in vm.calorieArray">
    {{value}}<br />
</div>

but nothing shows up on the page. Does anyone see the problem?

5
  • looks right to me, it's probably something else, you need to post more. Did you declare ng-app? Commented Nov 2, 2015 at 3:00
  • 1
    what are you seeing in console? Commented Nov 2, 2015 at 3:15
  • Use ng-repeat="value in calorieArray" instead of ng-repeat="value in vm.calorieArray" Commented Nov 2, 2015 at 3:40
  • @Shohel, i think OP used controller as syntax, so $scope not needed Commented Nov 2, 2015 at 4:11
  • write plunker or fiddle Commented Nov 2, 2015 at 4:28

2 Answers 2

2

The issue could be because of duplicate values not being allowed in the ng-repeat. See this plunker for an example of the issue. (Open up your dev console to see the JS errors) The description of the error can be found here.

In short you should use 'track by $index' as in the following:

<div ng-repeat="value in calorieArray track by $index">
    {{value}}<br />
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a fiddle with your example working: http://jsfiddle.net/JKBbV/743/

The controller is as simple as:

myApp.controller("myCtrl", function($scope) {
    $scope.values = [0,0,0,0,0.1];
});

My guess is that your app is not initialising correctly, or you aren't properly binding your array to the $scope.

1 Comment

if you use not old angular version you get error: Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: value in values, Duplicate key: number:0, Duplicate value: 0 : jsfiddle.net/JKBbV/742

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.