Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-ui-router only loads template but not controller not loaded

I tried to migrate my Angular app from using ngRoute to uiRoute. Everything works fine except the controllers. They simple don't load like they did with ngRoute and I don't get why. As I see it from uiRoute it should work like with ngRoute but it doesn't. There is no exception thrown. I also don't find any example for a similar configuration like mine although it's very simple. I home somebody can tell me where my controller is hiding.

http://plnkr.co/edit/VGyi3AxgslgpvwBCTkXI?p=preview

So as for ngRoute the controller should be accessible through 'homepage' but it looks like it's just empty :(

;(function(angular) {
'use strict';

 angular
 .module('MainRouter', ['ui.router'])
 .config(['$stateProvider', function($stateProvider) {
  $stateProvider
    .state('home', {
      url: '',
      views: {
        "mainView": {
          templateUrl: 'home.html'
        }
      },
      controller: 'HomepageCtrl',
      controllerAs: 'homepage'
    });
 }]);
})(angular);
like image 797
DaSch Avatar asked Mar 03 '26 02:03

DaSch


1 Answers

When you use views option inside the state definition, then ui-router engine doesn't look for templateUrl & controller defined in state level, basically you need to provide controller & templateUrl value from the namedView definition only. In your case you should add controller in mainView definition object.

Code

.config(['$stateProvider', function($stateProvider) {
  $stateProvider
    .state('home', {
      url: '',
      views: {
        "mainView": {
          templateUrl: 'home.html',
          controller: 'HomepageCtrl',
          controllerAs: 'homepage'
        }
      },
    });
}]);

Demo Plunkr

like image 110
Pankaj Parkar Avatar answered Mar 05 '26 16:03

Pankaj Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!