Подтвердить что ты не робот

Ui-router с привязкой ControllerAs

ui-router Состояние:

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

В DashboardController у меня есть:

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

И в шаблоне dashboard.html:

{{vm.title}}

Почему результат показывает "{{vm.title}}" вместо привязки к нему значение в контроллере?

4b9b3361

Ответ 1

При настройке состояния есть параметр controllerAs.

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

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

Ответ 2

В вашем controller function вам нужно будет return this; в конце функции.

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

Если мы работаем с $scope вместо vm = this;:

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

Удачи.