1

I have 3 classes in my ng2 Application. The first class manages, which directive is being used by an *ngIf, which by default includes the second class. And in the template of the second class I want to change the directive name from the first class, which would then trigger the first class to recheck the if statement and than select the third class.

Or generally said, is it bad practise to check with *ngIf for the directive to use?

Edit:

as stated by Günter Zöchbauer, here is some Code for what i try to do:

/app/app.component.ts:

template: `
    <div *ngIf="template == 'menu'">
        <menu></menu>
    </div>
    <div *ngIf="template == 'entry'">
        <entries></entries>
    </div>
`
[...]
export class AppComponent {
    public template = "menu";

    public setTemplate(newTemplate: string) {
        this.template = newTemplate;
    }
}

/app/menu/menu.component.html:

[...]
(click)="setTemplate('entry')"
[...]

Now I am looking for something to call setTemplate('entry') from the app.component.ts instead of menu.component.ts

1
  • 1
    Can you please add some code that demonstrates what you try to accomplish (components with templates)? Commented Feb 13, 2016 at 18:45

2 Answers 2

1

I think you should use an EventEmitter inside the menu component, so your source could look like this:

/app/menu/menu.component.html:

[...]
<div (click)="fireMyEvent('categories')">Categories</div>
<div (click)="fireMyEvent('entries')">Entries</div>
[...]

@Output() myEvent: EventEmitter<string> = new EventEmitter();
fireMyEvent(evtParam: string) {
    this.myEvent.emit(evtParam);
}

/app/app.component.ts:

template: `
    <menu (myEvent)="setTemplate($event)"></menu>
    <div *ngIf="template == 'categories'">
        <categories></categories>
    </div>
    <div *ngIf="template == 'entries'">
        <entries></entries>
    </div>
`
[...]

Or what about handling the click event of the menu component itself from the outside?

/app/app.component.ts:

    <div *ngIf="template == 'menu'">
        <menu (click)="setTemplate('menu')"></menu>
    </div>
Sign up to request clarification or add additional context in comments.

11 Comments

it should be this.myEvent.emit(null). Right?
Yeah, thank you very much, that's precisely what I needed to do!
But what, if I would need to pass the new template to fireMyEvent() so that it's not hardcoded setTemplate('menu')
You can supply a value to the event by myEvent.emit("something")
Yes, I should've mentioned $event.
|
0

You can add a template variable #menu.

If the tag where you add it is a native element, it hold a reference to the DOM element. If it is an Angular component you get a reference to the component.
In expressions you can reference it like

<menu #menu (click)="menu.setTemplate($event)"></menu>

3 Comments

well, but how does that will work? I have the *ngIf in the /app/app.component.ts and the menu and menu template is in app/menu/. So where should i exactly add that?
I can't see from your question or from the comments where you want to call it from. If the element is removed because the expressin in ngIf is false then there is nothing to call from or to because it doesn't exist.
The solution from Tamas Hegedus seems to work fine for what I need, thanks anyway for your effort!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.