Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  13] [ 1]  / answers: 1 / hits: 34129  / 9 Years ago, sun, april 19, 2015, 12:00:00

I'm doing something wrong yet I cannot see what (this is probably due to my low AngularJS skills).
I have a simple ng-repeat in my HTML:



<ul>
<li ng-repeat=fot in fotografia><img src={{fot.path}}></li>
</ul>


and here is my app.js:



myApp.controller('homeController', function($scope) {
// fotografia = []; is here only because I get an error otherwise,
// which means my later for loop can't access the $scope.fotografia

fotografia = [];
$scope.fotografia = [
{path:'img/fotografia/fot_1.jpg'},
{path:'img/fotografia/fot_2.jpg'}
];

// I want to add more images with this:
for(var i=0; i<5; i++) {
fotografia.push({
path: 'img/fotografia/fot_'+[i]+'.jpg'
});
}
});


Ng-repeat works fine with the 2 images I already have in my array (fot_1.jpg, fot_2.jpg). The loop is the the problem. How do I go about pushing more items into my array?


More From » angularjs

 Answers
28

Just push them onto the array in the scope. angular will then update the view.



for(var i=0; i<5; i++) {
$scope.fotografia.push({
path: 'img/fotografia/fot_'+[i]+'.jpg'
});
}

[#67013] Thursday, April 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;