1

I load my pages and menu from the angular scope witch is in turn requesting data to the Node server connected to the DB.

So my controller looks like this;

mid.controller('pageCtrl', function($scope, $http, $routeParams){
  $scope.id = ($routeParams && $routeParams["id"]) ? $routeParams["id"] : 0;
  $scope.pages = [];

  $http({
    method: 'GET',
    url: 'http://127.0.0.1/pages/'
  })
    .then(function(pages) {
      $scope.pages = pages.data;
   })
    .catch(function(errRes) {
      // Handle errRess
  });
});

The problem is that the request get executed all the way to my DB to for each menu element.

I have noticed that it get executed twice when using ng-repeat;

<div ng-repeat="page in pages" ng-cloak class="nav-item" >
<a href="#/{{page.view}}/{{page.xid}}">{{page.name}}</a>
</div>

Because there is 2 documents in my list.

And then it get executed another time to load the document in the main container.

How do I avoid that?

It might not be best practice but as it is right now I am filling the scope with all my pages from the DB and I was expecting to navigate inside the scope instead of the DB.

7
  • which line of code makes call to http request. You have given it directly in the controller. It will be called directly when the controller is loaded. Commented Jan 22, 2017 at 3:14
  • How can I check if scope.pages[] is empty? and set a condition Commented Jan 22, 2017 at 3:14
  • where do you want to check? in the pageCtrl ? Commented Jan 22, 2017 at 3:15
  • Yes something like if(scope.pages = empty) {we GET it} else we dont Commented Jan 22, 2017 at 3:16
  • 1
    yes, you can use $scope.page.length. but whenever you call controller function $scope.page.length will be zero. I think you are calling pageCtrl many times . Commented Jan 22, 2017 at 3:44

0

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.