0

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?

7
  • You seem to have a custom system for loading pages content... Is there a reason for you not to use ui-router, or the standard route provider in Angular? Also, your $http.get should be placed in a outside factory/service and called from within your controller. Commented Jul 13, 2014 at 0:45
  • It looks like your init function will get called the first time the controller is initialized, i.e., the first time you reference it in ng-controller or load a route that uses it. I don't see anyplace else that it would get called, and certainly not when you call loadPage, unless the content loaded by loadPage contains an ng-controller directive. Could you give more details about the order you're seeing things happen, and where and how you use this controller? Commented Jul 13, 2014 at 1:02
  • I can't recreate your issue: plnkr.co/edit/v7ooXMcLICD9l8NNEqOS The button press doesn't re-call init() Commented Jul 13, 2014 at 1:06
  • @KleberS: Let me guess: you are using ngClick on an <a>. Commented Jul 13, 2014 at 6:40
  • @ExpertSystem yes, I do. Is there any problem with that? Commented Jul 14, 2014 at 8:49

1 Answer 1

1

In Angular, when you want to have an <a> element display like an <a> element, but used differently (e.g. as a button (by attaching some ngClick directive)), you should use an empty href:

<a href="" ng-click="doSomething()">Do something</a>

The problem with using href="#" is that it is eihter:

  1. Recognized by ngRoute (if it is used) as a "link" to the home route ('/'), so even if you are in that route it gets reloaded causing the effects of the ngClick callback to be voided or

  2. Recognized by the browser as link with an anchor tag to the same page, so clicking it causes the page to reload (again voiding any effect of the ngClick callback).


I am not sure how ui-router handles this, but I am not a big fan of ui-router - maybe someone with more experience can give some feedback.

Sign up to request clarification or add additional context in comments.

Comments

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.