1

I have the following routing config

myApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'partials/list.html',
        controller: 'contactListCtrl'
      }).
      when('/new', {
        templateUrl: 'app/partials/form.html',
        controller: 'contactAddCtrl'
      }).
      otherwise({
        redirectTo: '/'
      });
  }]);

my home page('/') simply loads a number of contacts and its contactListCtrl is like

myApp.controller('contactListCtrl', function($scope, $http) {
    $http.get("list")
            .then(function(response) {
              $scope.contact_list = response.data;  
            });
});

So when I load the home page on browser, it loads the template and then send another request ('/list') to server to get the contacts (json). so the browser sends 2 requests to the server.

Is this normal in AngularJS development to send 2 requests simultaneously to get template along with data? Is there any work around to send only 1 request to get both template and data ?

If I need send 2 requests in this case and suppose I have to load 5 templates in home page then I have to send another 5 requests to bind data (from database) to each template ?

4
  • I think you have mentioned ng-controller also in your template Commented Jan 25, 2016 at 11:10
  • @sasikumar sorry not understand what u mean.. Commented Jan 25, 2016 at 15:54
  • Have you mentioned ng-controller="contactListCtrl" in Template also? , because in Route configuration also contains controller name related to templateURL, that is why it's trying to load two times Commented Jan 25, 2016 at 15:57
  • @saikumar actually I have not specified ng-controller any where, where should I specify ng-controller ? and what value ? thanks Commented Jan 25, 2016 at 17:14

1 Answer 1

1

You can use resolve in the route, that ill load the data and than load your template. You can check here for solution http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

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.