gpt4 book ai didi

javascript - 在 Angular 1.x.x ES6 中注入(inject)工厂

转载 作者:行者123 更新时间:2023-12-02 15:00:39 25 4
gpt4 key购买 nike

我使用 Angular 1.x.x 和 ES6 语法。

我有一个像这样的 Controller :

    class BuilderController {  

constructor(Auth) {
this.foo = 'bar';
}

create() {
console.log(Auth); //this is undefined
}
}

angular.module('myapp')
.controller('BuilderCtrl', BuilderController);

我正在尝试将 Auth 工厂注入(inject)到我的 Controller 中,但如果我在 create() 方法中控制台登录 Auth,则它是未定义的。

有人可以向我解释一下如何在 Angular Controller (类)中正确注入(inject)工厂吗?

最佳答案

您应该将其用作带有关键字 this 的全局属性:

class BuilderController {  

constructor(Auth = "default") {
this.foo = 'bar';
this.Auth = Auth;
}

create() {
console.log(this.Auth); //if Auth is undefined then "default" gets logged.
}
}

关于javascript - 在 Angular 1.x.x ES6 中注入(inject)工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35451945/

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