ui-router with ControllerAs binding

三世轮回 提交于 2019-12-22 03:51:03

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/29392293/ui-router-with-controlleras-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!