I have the following controller code:
applicationControllers.controller('PostsController', ['$scope', '$http', function
($scope, $http) {
var page = 1;
$scope.init = function() {
this.loadPage(page);
}
$scope.nextPage = function() {
page++;
this.loadPage(page);
}
$scope.previousPage = function() {
page--;
if (page <= 0) { page = 1 }
this.loadPage(page);
}
$scope.filterByProvince = function(provinceName) {
console.log(provinceName);
}
$scope.loadPage = function(page) {
$http.get('/posts.json?page=' + page).success(function(data) {
$scope.posts = data;
});
}
$scope.init();
}]);
The problem is when using a ng-click directive to filterByProvince('test'), it seems the init function are also called. I want to avoid this behaviour.
Any help?
initfunction will get called the first time the controller is initialized, i.e., the first time you reference it inng-controlleror load a route that uses it. I don't see anyplace else that it would get called, and certainly not when you callloadPage, unless the content loaded byloadPagecontains anng-controllerdirective. Could you give more details about the order you're seeing things happen, and where and how you use this controller?ngClickon an<a>.