我如何在同一组件ts中的save()méthode中使用getComapny()

时间:2018-01-17 11:55:10

标签: angular

如何在同一组件ts中的save()méthode中使用getComapny()?

CompanyUserDialogComponent.ts

@Component({
    selector: 'jhi-company-user-dialog',
    templateUrl: './company-user-dialog.component.html'
})

export class CompanyUserDialogComponent implements OnInit {
    constructor(
    ) {     
    }
    save() {
        console.log(this.user)
        this.doNotMatch = null;
        this.isSaving = true;
        if (this.user.password !== this.confirmPassword) {
            this.doNotMatch = 'ERROR';
        }else if (this.user.id !== null) {
            this.userService.update(this.user).subscribe((response) =>
             this.onSaveSuccess1(response), () => this.onSaveError());
        } else {
            this.userService.create(this.user).subscribe((response) =>{
                console.log(response);

                this.onSaveSuccess1(response), () => this.onSaveError()
            });
        }
    }

CompanyUserPopupComponent.ts

@Component({
    selector: 'jhi-company-user-popup',
    template: ''
})
export class CompanyUserPopupComponent implements OnInit, OnDestroy {
    a:any
    routeSub: any;
    constructor(
        private route: ActivatedRoute,
        private companyPopupService: CompanyPopupService,
        private userModalService: UserModalService,
        private companyService: CompanyService,

    ) {}

    ngOnInit() {
        this.routeSub = this.route.params.subscribe((params) => {
            this.getComapny(params['id'])
            if ( params['login'] ) {
                this.userModalService.open(CompanyUserDialogComponent as Component, params['login']);
            } else {
                this.userModalService.open(CompanyUserDialogComponent as Component);
            }
        });     
    }
    getComapny(id:number){

        this.companyService.find(id).subscribe(res=>{
            console.log(res)
        })
      }

    ngOnDestroy() {
        this.routeSub.unsubscribe();
    }
}

1 个答案:

答案 0 :(得分:0)

由于getCompany中的CompanyUserPopupComponent方法不依赖于组件,因此您只需在构造函数中注入服务即可使用getCompany的{​​{1}}并使用CompanyService

中的方法

CompanyUserDialogComponent.ts:

CompanyUserDialogComponent

希望这会有所帮助:)