将ES6类与角度服务/控制器一起使用时出错

SET*_*SET 11 angularjs ecmascript-6

我想在Angular应用程序中使用ES6类并试图像这样使用它:

'use strict';

class EditorCtrl{
    constructor(){
        this.something = "ASd";
    }
    foo(){

    }
}
angular.module('Editor').controller('EditorCtrl', EditorCtrl);
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,这段代码给了我一个错误:Class constructors cannot be invoked without 'new'.为什么会发生这种情况以及如何解决这个问题?

Angular:1.4.7 Chrome:46.0​​.2490.71

小智 2

'use strict';

class EditorCtrl{
    constructor($scope){
        this.something = "ASd";
    }
    foo(){

    }
}
// Use this instead.
angular.module('Editor').controller('EditorCtrl', ['$scope', function($scope) {
    return new EditorCtrl($scope);
}]);
Run Code Online (Sandbox Code Playgroud)

由于错误通知必须返回“新”。这样注入也能起作用。干杯。

  • 这根本不是推荐的处理方式(不要直接分配给 `$scope`),也不是记录的方式(控制器应该能够成为类,因为它们是新的) (2认同)