Using 'controller as' with the ui-router isn't working as expected Using 'controller as' with the ui-router isn't working as expected angularjs angularjs

Using 'controller as' with the ui-router isn't working as expected


In your state configuration :

Instead of controller: 'Title as t', try :

controller: 'Title',controllerAs: 't'

Edit : Just implemented a minimal app with ui-router and the syntax controller: Title as t also works, in versions 0.2.0 of ui-router to the most recent one as of today. I can see the t instance when I inspect angular scopes.


Your controller needs to return the value of this in order for the controllerAs feature to work properly. Since CoffeeScript implicitly returns the last line, you need to write:

return this

or if you are using the vm syntax and have written:

vm = this

you can write at the very end of the controller:

return vm


If this helps anyone my problem came about from using templated views but specifying the controllerAs outside the views element. This took forever to figure out. Credit to this thread https://github.com/driftyco/ionic/issues/3058

** WRONG **

views: {'content@': { templateUrl: 'views/listing.html' }},controller: 'ListingCtrl',controllerAs: 'ctrl'

** RIGHT **

views: {  'content@': { templateUrl: 'views/listing.html' },   controller: 'ListingCtrl',   controllerAs: 'ctrl'}