Oswaldo
Oswaldo

Reputation: 3376

ui-router with ControllerAs binding

ui-router State:

$stateProvider
    .state('dashboard', {
        url: '/dashboard',
        templateUrl: 'app/dashboard/dashboard.html',
        controller: 'DashboardController as vm'
    });

In DashboardController I have:

var vm = this;
vm.title = 'Dashboard';

And in dashboard.html template:

{{vm.title}}

Why the result is showing "{{vm.title}}" instead of bind to it's value in controller?

Upvotes: 13

Views: 11558

Answers (2)

Aakash
Aakash

Reputation: 23737

In your controller function, you will have to return this; at the end of the function.

var vm = this;
vm.title = 'Dashboard';
// 
return vm;

If we work with $scope instead of vm = this;:

$scope.title = 'Dashboard';
// 
return $scope;

Good Luck.

Upvotes: 1

Brad Barber
Brad Barber

Reputation: 2963

There's a controllerAs setting when you configure the state.

$stateProvider
    .state('dashboard', {
        url: '/dashboard',
        templateUrl: 'app/dashboard/dashboard.html',
        controller: 'DashboardController',
        controllerAs: 'vm'
    });

https://github.com/angular-ui/ui-router/wiki

Upvotes: 25

Related Questions