Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i call a certain method from the class decorator when some `*ngIf` condition is met?

How can i call a certain method from the class decorator when some *ngIf condition is met. I have a scenario exactly like this AngularJS question in which ng-init() is used but ng-init is not part of Angular2

<div *ngIf="obj.someProperty" callSomeMethod() >
 <!--render inner parts-->
</div>
like image 578
ska.dev Avatar asked Apr 18 '16 23:04

ska.dev


People also ask

How do you call a function after ngIf?

Try *ngIf="condition && yourfunction()" . Your function must return true to the if evaluate to true, but it will only be executed if your condition is true, since an and operator will stop on first false. I would advise against this. This is very bad practice and will cause serious performance issues in your codebase.

What is * in * ngIf?

The * syntax means that ngIf is a structural directive, meaning that it affects the structure of the page.

What is the use of * ngIf in angular2?

The *ngIf directive is most commonly used to conditionally show an inline template, as seen in the following example. The default else template is blank.

What is the difference between * ngIf and ngIf?

ngIf is the directive. Because it's a structural directive (template-based), you need to use the * prefix to use it into templates. *ngIf corresponds to the shortcut for the following syntax (“syntactic sugar”): <template [ngIf]="condition">


2 Answers

It depends on what callSomeMethod() is doing, but one possibility is to add a directive to the *ngIf element, and execute that logic in the OnInit hook of that directive.

<div *ngIf="obj.someProperty" some-method-directive>
   <!--render inner parts-->
</div>

And elsewhere:

@Directive({
   selector='[some-method-directive]',
})
class SomeMethodDirective implements OnInit { // OnInit imported from angular2/core

   ngOnInit(){
      // insert your someMethod lofic
   }
}

If you need access to the parent component in this method, you can get ahold of it via constructor injection in the directive:

constructor(@Host(ParentComponent) private parent: ParentComponent){ }

and you'll then have access to it via this.parent.

This is the most analogous approach I can think of to the ng1 approach, but like I said - depending on what someMethod() needs to accomplish, it might not be a feasible solution. If not, please comment/edit your question to explain why, and that'll give me a better idea of what we're doing here.

like image 138
drew moore Avatar answered Oct 16 '22 07:10

drew moore


It is possible using customized ngIf directive and template syntax:

<template [ngCondition]="obj.someProperty" (show)="callSomeMethod()">
 <h3 >if callback!</h3>
</template>

you should be able to add callbacks for true/false (show/hide) conditions.

Directive source:

@Directive({
    selector: '[ngCondition]'
})
export class NgCondition
{
    @Output('show')
    private show:EventEmitter<any> = new EventEmitter<any>();

    @Output('hide')
    private hide:EventEmitter<any> = new EventEmitter<any>();

    private _viewContainer:ViewContainerRef;
    private _templateRef:TemplateRef;
    private _prevCondition:any;

    constructor(_viewContainer:ViewContainerRef, _templateRef:TemplateRef)
    {
        this._viewContainer = _viewContainer;   
        this._templateRef = _templateRef;
        this._prevCondition = null;
    }

    @Input()
    public set ngCondition(newCondition:boolean)
    {
        if (newCondition && (isBlank(this._prevCondition) || !this._prevCondition))
        {
            this._prevCondition = true;
            this._viewContainer.createEmbeddedView(this._templateRef);
            this.show.emit({});
        }
        else if (!newCondition && (isBlank(this._prevCondition) || this._prevCondition))
        {
            this._prevCondition = false;
            this._viewContainer.clear();
            this.hide.emit({});
        }
    }
}
like image 20
kemsky Avatar answered Oct 16 '22 06:10

kemsky