I have the following state setup for a page using an abstract state and the controller as syntax:
# Details page route
.state 'title',
url: '/title',
abstract: true,
template: '<ui-view/>',
.state 'title.show',
url: '/:titleId',
templateUrl: 'title/show.html'
controller: 'Title as t'
For the purpose of this demo lets say I assign a variable to the 't' instance of the 'Title' controller and I do this inside of the Title controller function.
angular.module('title').controller 'Title',
['$state', ($state) ->
this.name = 'Test'
and inside my view 'title/show.html' I attempt to print out that variable I just created to the page:
{{t.name}}
I don't see anything. But if I remove the controller our of the ui-router and onto the element that wraps the 'title/show.html' page like this:
<div ng-controller="Title as t">
Then everything works great. Has anyone come across this problem before. I have it working fine in another app so I'm still trying to figure out what might be different in this app, maybe a difference in js library versions.
You will have to
return this;
at the end of yourcontroller function
for thecontrollerAs
syntax to work.If you are working with
$scope
, you'll have toreturn $scope
instead.Good Luck.
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 **
** RIGHT **
In your state configuration :
Instead of
controller: 'Title as t'
, try :Edit : Just implemented a minimal app with
ui-router
and the syntaxcontroller: Title as t
also works, in versions 0.2.0 ofui-router
to the most recent one as of today. I can see thet
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:or if you are using the vm syntax and have written:
you can write at the very end of the controller:
Seems like these answers might be working for a lot. I came here with a different problem :
In my
UI Router
Javascript file, mycontrollers
are defined like this :And in my template file if I try to access the controller with the name
groupHomeController
it is not able to access.But on the other hand when I changed my code to this :
It works perfectly fine.