2

Trying to call a create-tag modal from a different Component.

The component I am working in is called pets and is located in: app/main/ClientPatientManagement/Pets

Trying to access the show() method in: app/main/tags/tags/create-or-edit-tag-modal.component'

Here is the code in my Pets.Component:

    import { CreateOrEditTagModalComponent } from '@app/main/tags/tags/create-or-edit-tag-modal.component';
@Component({
    templateUrl: './pets.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class PetsComponent extends AppComponentBase {

    @ViewChild('createOrEditTagModal', { static: true }) createOrEditTagModal: CreateOrEditTagModalComponent;

Here are some other code including ngOnInit etc. but I want to call the below function when a button is pressed:

createTag(): void {
        this.createOrEditTagModal.show();
    }

My problem is that I keep getting an error saying my this.createOrEditTagModal is undefined.

I have also imported this component in my main.module.ts and added it to my declarations there.

Can anyone help me as to why this keeps being undefined?

EDIT: For completeness, I added some code from create-or-edit-tag-modal-component

@Component({
    selector: 'createOrEditTagModal',
    templateUrl: './create-or-edit-tag-modal.component.html'
})
export class CreateOrEditTagModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    constructor(
        injector: Injector,
        private _tagsServiceProxy: TagsServiceProxy
    ) {
        super(injector);
    }


    show():void{
        console.info("Called tag component successfully!"); //for testing
    }

3 Answers3

2

Good news, found the problem!

the problem was in my pets.component.html. I forgot to declare my modal there, so I added this in pets.component.html:

<createOrEditTagModal #createOrEditTagModal (modalSave)="getPets()"></createOrEditTagModal>

This manage to fix the issue, running now smoothly and error free. Note that the getPets() function gets called after the modal's own save function, so it just basically refreshes the page.

1

I think this first parameter is wrong in your code.

@ViewChild('createOrEditTagModal', { static: true }) createOrEditTagModal: CreateOrEditTagModalComponent;

Should be changed as

@ViewChild(CreateOrEditTagModalComponent, { static: true }) createOrEditTagModal: CreateOrEditTagModalComponent;

Also, make sure pets.component.html has CreateOrEditTagModalComponent in it.

0

Can you please try to give a Directive decorator like this :

@Directive({selector: 'createOrEditTagModal'})
export class CreateOrEditTagModalComponent extends AppComponentBase {