Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Instead of doing two $http requests, one for the controller data and one for the view data, how come I can't just load a view and have a controller embedded in the view? It doesn't work.

My router code:

app.config(function($stateProvider, $urlRouterProvider){

      $urlRouterProvider.otherwise("/admin");
       $stateProvider.state('home', {
    url: "",
    templateProvider: function($http, $stateParams) {
        return $http({
        method: 'GET',
        url: '/admin/home'
      }).then(function successCallback(html) {
        return html.data;
      });
       },
     controller: function($scope) {
          // $scope.activeSection.activeSection = "notNone";
        }
  })

  //all other states go here

    });

My view returned from the templateProvider $http promise:

<div class="container" ng-controller="home">
{{orders[0]}}
</div>

<script>
app.controller('home', ['$scope', '$http', '$state', function($scope, $http, $state) {
$scope.orders = <?php echo json_encode($data[0]); ?>;
}]);
</script>

But I get an error saying "home" is undefined. I understand I can just set the controller on the route and do a $http request from there but it seems silly if I can just get what I need from a controller standpoint already in the view. Right? Or am I missing something. It would make it easier on the server to not have multiple routes (view and controller) for what is essentially one route.

share|improve this question
1  
What's wrong with controller: 'home', templateUrl: '/admin/home' and discarding ng-controller? – Phil Aug 19 at 1:29
1  
Yeah this doesn't really make sense... typically all of your JS ends up getting bundled into a single file in the end, if you want to you can setup build tools that will also incorporate all the html templates into that one JS file this way you have one request for the index.html and then 1 request to get everything else, trying to put the template into the script won't help... also much better if you setup a JSON API with PHP instead of having it write code into the front end – shaunhusain Aug 19 at 1:31
    
@Phil It's not a template as in ng-template it's html served from a separate file and the controller needs to fetch a set of data from the database. Instead of doing that in a separate request I'd like to do that when the view is generated from the template provider. – Summer Developer Aug 19 at 1:33
    
You cannot simply dynamically add controllers like that. Your controllers / routes should fetch the data required, when required – Phil Aug 19 at 1:40
    
@Phil Alright that's what I was afraid of, just too bad because now I need to specify in the route another $http request which is another route for the server... – Summer Developer Aug 19 at 1:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.