0

how can i use getComapny() in save() méthode in the same component ts ?

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();
    }
}
Mohamed Ali RACHID
  • 3,245
  • 11
  • 22
wajdi
  • 1
  • 1
  • If these components are siblings you can try this https://stackoverflow.com/a/37587862/1876572 or https://angular.io/guide/component-interaction – Eldho Jan 17 '18 at 12:03
  • Otherwise `CompanyUserDialogComponent ` must use the companyservice and call the service to find the method. – Eldho Jan 17 '18 at 12:04

1 Answers1

0

Since getCompanymethod in CompanyUserPopupComponent doesn't depend on the component , you can simply use the getCompany of your CompanyService by injecting the service in the constructor and use the method in CompanyUserDialogComponent

CompanyUserDialogComponent.ts :

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

export class CompanyUserDialogComponent implements OnInit {
    constructor(private companyService: CompanyService) {     
    }
    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()
            });
        }
    }

Hope this will help :)

Mohamed Ali RACHID
  • 3,245
  • 11
  • 22