将controller和controllera设置为相同的字符串

Set controller and controllerAs to the same string

本文关键字:字符串 设置 controller controllera      更新时间:2023-09-26

假设你有一个AngularJS指令,你想把controller属性和controllerAs属性设置为相同的字符串。

angular.module('blahModule', [])
.directive('blahDirective', function(){
    return {
        restrict: 'E',
        controller: 'blahController',
        controllerAs: 'blahController',
        templateUrl: 'blah/blah.html'
    }
});

上面是有效的,但是当controllercontrollerAs值设置为相同的值时,感觉有点多余。有没有办法在一个属性中做到这一点?如:

angular.module('blahModule', [])
.directive('blahDirective', function(){
    return {
        restrict: 'E',
        controllerAndControllerAs: 'blahController',
        templateUrl: 'blah/blah.html'
    }
});

提前感谢!

可以使用

controller: 'blahController as blahController',