I have an issue with AngularJS ng-view and ng-repeat, and I believe it has to do with the way I handle the scope. I use ng-repeat to show a list of items present in an array. And a button that is outside the ng-repeat scope on ng-click updates the array. But some how I am not able to get this feature working. Here is a sample of my code:
<!-- home.html partial -->
<h1>Home view</h1>
<div>
<ul ng-repeat="p in posts.data">
<li>{{p.title}}</li>
</ul>
<a href="#" ng-click="updatePosts(5)">More</a>
</div>
Here is the controller that is bound to this partial file:
var app = angular.module('plunker', ['ngRoute']);
app.config(function($routeProvider){
$routeProvider
.when('/', {
controller: 'testController',
templateUrl: 'home.html'
})
.otherwise({ redirectTo: '/' });
});
app.controller('testController',
[
'$scope',
function ($scope)
{
$scope.posts = {
data : []
};
$scope.getPosts = function(count)
{
for(i =0; i<count; ++i)
{
$scope.posts.data.push({title : "Title-"+i})
}
}
$scope.updatePosts = function(count)
{
var offset = $scope.posts.data.length;
var data = [];
for(i =offset; i<offset+count; ++i)
{
data.push({title : "Title-"+i});
}
$scope.posts.data = $scope.posts.data.concat(data)
}
$scope.getPosts(5);
}
]
);
The feature that I want is when one clicks "More" link on the main page, the controller should get little snippets of code and update the view with more data. For instance, I would expect the view to go from (1) to (2) on clicking "More". But I always end up with just (1) no matter what.
(1)
Home View
- Title-0
- Title-1
- Title-2
- Title-3
- Title-4
More
(2)
Home View
- Title-0
- Title-1
- Title-2
- Title-3
- Title-4
- Title-5
- Title-6
- Title-7
- Title-8
- Title-9
More
I also have the above example setup in Plnkr here
** update**
I tried the same code without using ng-view and the code works exactly as expected. So I am sure that the problem that I am encountering has to do with my usage of ng-view. An example in Pnkr setup without using ng-view is shown here