gpt4 book ai didi

angularjs - 将 ES6 类与 Angular 服务/ Controller 一起使用时出错

转载 作者:行者123 更新时间:2023-12-02 19:35:15 24 4
gpt4 key购买 nike

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

'use strict';

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

}
}
angular.module('Editor').controller('EditorCtrl', EditorCtrl);

但由于某种原因,这段代码给了我一个错误:没有“new”就无法调用类构造函数。为什么会发生这种情况以及如何解决这个问题?

Angular :1.4.7Chrome:46.0​​.2490.71

最佳答案

'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);
}]);

错误提示必须返回“new”。这样注入(inject)也能起作用。干杯。

关于angularjs - 将 ES6 类与 Angular 服务/ Controller 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com