相关疑难解决方法(0)

Angular2中的工厂相当于什么?

所以我习惯在Angular中使用工厂和服务.

我正在阅读Angular2文档,我没有看到任何工厂的等价物.Angular2的等价物是什么?

angularjs angular

13
推荐指数
3
解决办法
3万
查看次数

角圆依赖警告

tl; dr:向下滚动到解决方案

我有一个循环依赖项,并且正在收到警告,这是正确的,但是,我正在对其进行管理。问题是我有一个聊天组件。在角落,您可以选择查看他们的个人资料页面,而在他们的个人资料页面中,您可以选择向他们发送消息,因此是循环依赖性。我正在通过管理

聊天组件

public async openProfile(): Promise<void> {
  this.modalCtrl.dismiss(); //closing the chat component before opening the profile modal
  const profile = await this.modalCtrl.create({
    component: ProfileComponent,
  });
  await profile.present();
} 
Run Code Online (Sandbox Code Playgroud)

profile.component

public async openChat(): Promise<void> {
  this.modalCtrl.dismiss(); //closing the profile component before opening the chat modal
  const chat = await this.modalCtrl.create({
    component: ProfileComponent,
  });
  await chat.present();
} 
Run Code Online (Sandbox Code Playgroud)

有没有更简单的方法来处理这种循环依赖关系?

更新:根据下面的建议,我尝试创建服务。但是现在我有了一个三向依赖圈:

聊天组件

private modalService: ModalService;

constructor(modalService: ModalService){
  this.modalService = modalService
}

public async openProfile(): Promise<void> {
  this.modalService.openProfile(this.userData);
} 
Run Code Online (Sandbox Code Playgroud)

profile.component

private modalService: ModalService; …
Run Code Online (Sandbox Code Playgroud)

ionic-framework angular

5
推荐指数
1
解决办法
144
查看次数

如何使用 Injector 而不是单例来获取类的新实例

我的 angular 应用程序中有两个可注入的类

@Injectable()
class B {}

@Injectable()
class A {
  constructor(b:B) { }
}
Run Code Online (Sandbox Code Playgroud)

我希望 A 类为单例,B 类为瞬态

我开始知道我可以在 A 类中使用ReflectiveInjector.resolveAndCreate来获取 B 类的实例。有没有更好的方法来实现这一点?

dependency-injection angular

3
推荐指数
1
解决办法
2611
查看次数

创建具有依赖项的类的新实例,而不是了解工厂提供程序

我已经在这方面工作了一段时间,似乎无法找到足够明白的答案.我有一个TestComponent,它使用TestService从服务器获取一组TestModel.当我抓住这些测试模型时,它只是一个json文件,服务器正在读取并使用正确的mime类型发回.从服务器获取测试模型后,我将它们放在一个简单的select元素下拉列表中.选择测试模型后,它会在嵌套组件TestDetailComponent中显示所选的测试模型.

这一切都很好,并且工作正常.当我从服务器提取数据时,我一直遇到问题.由于JavaScript没有运行时检查,我们无法自动将JSON从服务器转换为typescript类,因此我需要使用已检索的JSON手动创建TestModel的新实例.

好的,这就是问题所在.我需要调用新的TestModel并为其提供依赖项,但它需要是TestModel的新实例.我希望TestModel能够将自身保存并更新回服务器,因此它依赖于来自@ angular/core的Http,并且它依赖于我使用opaqueToken,CONFIG.I进行角度注入的配置类.无法弄清楚如何获得TestModel的新实例.这是初始文件

TestComponent:

import { Component, OnInit } from '@angular/core';

import { TestService } from './shared/test.service';
import { TestModel } from './shared/test.model';
import { TestDetailComponent } from './test-detail.component';

@Component({
    selector: "test-component",
    templateUrl: 'app/test/test.component.html',
    styleUrls: [],
    providers: [TestService],
    directives: [TestDetailComponent]
})
export class TestComponent implements OnInit {

    tests: TestModel[] = [];
    selectedTest: TestModel;

    constructor(private testService: TestService) {};

    ngOnInit() {
        this.testService.getTestsModels().subscribe( (tests) => {
            console.log(tests);
            this.tests = tests 
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

TestComponent模板:

<select [(ngModel)]="selectedTest">
    <option *ngFor="let test of tests" [ngValue]="test">{{test.testing}}</option> …
Run Code Online (Sandbox Code Playgroud)

dependency-injection typescript angular

2
推荐指数
1
解决办法
1万
查看次数