0

I have modified my question as per my observation.$http request on server side is not happening whenever the view is called second or third or so on times although initWishList (ng-init) is getting called. The scenario is as below:

I have a My Account tab in the nav-bar, which opens a view having options like MyWishlist, My Address etc. When My wishlist is clicked for the first time, $http request happens, but when I again click on My Account (this time I don't see any call on server side when the .html view is loaded) , and then if I click My Wishlist , the controller is called but $http isn't called (this time I again dont see any call on server side from $http) but I can see the alert Init Called

Why so?

$scope.initWishList = function(){
    alert('Init called')
    $http.get("/get_wish_list/")
    .success(function (response) {
        $scope.refreshWishList(JSON.parse(response["products_json"]));
    })
    .error(function(){
        console.log('Error');
    });
};

Hope I have explained the scenario properly.

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
  • ng-init should be not used. why you don't initate a variable in the scope controller and update it after ? – AlainIb Dec 21 '15 at 15:51
  • @AlainIb Hmm, but why is it not getting called when view is clicked again? – Shashank Vivek Dec 21 '15 at 15:52
  • Can you show HTML of your scenario? `ng-init` gets executed each time the element it is attached to is rendered and compiled by Angular, so it won't matter if the template is retrieved from the server or from cache. – tasseKATT Dec 21 '15 at 16:28
  • @tasseKATT: I have updated my question. Have a look now – Shashank Vivek Dec 21 '15 at 16:29
  • That code doesn't help. You need to show the HTML where `ng-init` is used, and how it relates to the nav-bar and views. – tasseKATT Dec 21 '15 at 16:30
  • "This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope." - from AngularJS's ngInit [documentation](https://docs.angularjs.org/api/ng/directive/ngInit) – JonK Dec 21 '15 at 16:34
  • @tasseKATT: I can surely put my html code but its all working fine. I mean even `ng-init` is getting called properly and I am able to see the alerts. Its just that the `$http` call(which is inside `ng-init`) is not getting triggered. Do u still want me to put `html` code ? It'll look messy after that :( – Shashank Vivek Dec 21 '15 at 16:36
  • @JonK: Even when I have removed `ng-init` and have called this function through ` $scope.initWishList();` , I am still not able to see `$http` call on server end – Shashank Vivek Dec 21 '15 at 16:41
  • No need for HTML if `ng-init` isn't the problem anymore :) – tasseKATT Dec 21 '15 at 16:48
  • 1
    Open the network tab of the Chrome DevTools and then call `initWishList` twice. You should be able to see if the second call is retrieved from cache. – tasseKATT Dec 21 '15 at 16:52
  • @tasseKATT: Awesome. Saw from Firefox. It is showing `/get_wish_list` **Transferred** column as `cached`. Thanks – Shashank Vivek Dec 21 '15 at 17:19
  • @tasseKATT: Although I have handled cache issue but I wonder why it is enabled by default ? I mean shouldn't this be disabled in case of `$http` calls as it was picking old data – Shashank Vivek Dec 23 '15 at 06:47
  • When you see a call in the Firefox/Chrome dev tools and it says `cached`, it has nothing to do with Angular. It's the browser that has cached the request. If you cache results with Angular the next call to `$http` would retrieve the result from an in memory object, and there would not be any visible call in dev tools. Not sure if that explains your question or not :) – tasseKATT Dec 23 '15 at 06:51

1 Answers1

1

Controllers gets intialized only once when angular parses ng-controller directive. Since controller is initialized once, ng-init will also run once.

For your satisfaction, add a break point in the your controller on the first line, see when you hit that break point. Are you hitting that break point, every time you click the tab ? If not, ng-init will not be called every time

Anand
  • 14,545
  • 8
  • 32
  • 44