how to manually invoke these events? ngOnInit not getting fired for the component injected using the...











up vote
0
down vote

favorite












ngOnInit not getting fired for the component injected using the component factory resolver



@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;

cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;

constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}

updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}





after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that











share|improve this question
























  • You've spelt ngOnInit incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit in it ?
    – Zze
    Nov 8 at 20:41












  • Sorry thats just a typo. i have corrected that thanks for the catch.
    – Aji
    Nov 8 at 21:12










  • Check this stack blitz reference - stackblitz.com/angular/…
    – Minu
    Nov 8 at 21:12










  • with this approch its working fine for mee too. the problem comes with this code
    – Aji
    Nov 8 at 21:14










  • Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
    – Minu
    Nov 8 at 21:19















up vote
0
down vote

favorite












ngOnInit not getting fired for the component injected using the component factory resolver



@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;

cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;

constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}

updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}





after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that











share|improve this question
























  • You've spelt ngOnInit incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit in it ?
    – Zze
    Nov 8 at 20:41












  • Sorry thats just a typo. i have corrected that thanks for the catch.
    – Aji
    Nov 8 at 21:12










  • Check this stack blitz reference - stackblitz.com/angular/…
    – Minu
    Nov 8 at 21:12










  • with this approch its working fine for mee too. the problem comes with this code
    – Aji
    Nov 8 at 21:14










  • Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
    – Minu
    Nov 8 at 21:19













up vote
0
down vote

favorite









up vote
0
down vote

favorite











ngOnInit not getting fired for the component injected using the component factory resolver



@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;

cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;

constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}

updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}





after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that











share|improve this question















ngOnInit not getting fired for the component injected using the component factory resolver



@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;

cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;

constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}

updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}





after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that








angular angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 22:45

























asked Nov 8 at 20:15









Aji

9410




9410












  • You've spelt ngOnInit incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit in it ?
    – Zze
    Nov 8 at 20:41












  • Sorry thats just a typo. i have corrected that thanks for the catch.
    – Aji
    Nov 8 at 21:12










  • Check this stack blitz reference - stackblitz.com/angular/…
    – Minu
    Nov 8 at 21:12










  • with this approch its working fine for mee too. the problem comes with this code
    – Aji
    Nov 8 at 21:14










  • Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
    – Minu
    Nov 8 at 21:19


















  • You've spelt ngOnInit incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit in it ?
    – Zze
    Nov 8 at 20:41












  • Sorry thats just a typo. i have corrected that thanks for the catch.
    – Aji
    Nov 8 at 21:12










  • Check this stack blitz reference - stackblitz.com/angular/…
    – Minu
    Nov 8 at 21:12










  • with this approch its working fine for mee too. the problem comes with this code
    – Aji
    Nov 8 at 21:14










  • Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
    – Minu
    Nov 8 at 21:19
















You've spelt ngOnInit incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit in it ?
– Zze
Nov 8 at 20:41






You've spelt ngOnInit incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit in it ?
– Zze
Nov 8 at 20:41














Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12




Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12












Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12




Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12












with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14




with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14












Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19




Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










When you manually create components using the ComponentFactoryResolver, you will have to manually call the Angular life cycle events that you are using on them.



Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit life cycle event, so it would be like the following:



updateComponent() {
if (!this.isViewInitialized) {
return;
}

if (this.cmpRef) {
this.cmpRef.destroy();
}

let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;

this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215471%2fhow-to-manually-invoke-these-events-ngoninit-not-getting-fired-for-the-componen%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    When you manually create components using the ComponentFactoryResolver, you will have to manually call the Angular life cycle events that you are using on them.



    Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit life cycle event, so it would be like the following:



    updateComponent() {
    if (!this.isViewInitialized) {
    return;
    }

    if (this.cmpRef) {
    this.cmpRef.destroy();
    }

    let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
    this.cmpRef = this.target.createComponent(factory)
    this.cmpRef.instance.data = this.stepinfo;

    this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
    }





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      When you manually create components using the ComponentFactoryResolver, you will have to manually call the Angular life cycle events that you are using on them.



      Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit life cycle event, so it would be like the following:



      updateComponent() {
      if (!this.isViewInitialized) {
      return;
      }

      if (this.cmpRef) {
      this.cmpRef.destroy();
      }

      let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
      this.cmpRef = this.target.createComponent(factory)
      this.cmpRef.instance.data = this.stepinfo;

      this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
      }





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        When you manually create components using the ComponentFactoryResolver, you will have to manually call the Angular life cycle events that you are using on them.



        Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit life cycle event, so it would be like the following:



        updateComponent() {
        if (!this.isViewInitialized) {
        return;
        }

        if (this.cmpRef) {
        this.cmpRef.destroy();
        }

        let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
        this.cmpRef = this.target.createComponent(factory)
        this.cmpRef.instance.data = this.stepinfo;

        this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
        }





        share|improve this answer












        When you manually create components using the ComponentFactoryResolver, you will have to manually call the Angular life cycle events that you are using on them.



        Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit life cycle event, so it would be like the following:



        updateComponent() {
        if (!this.isViewInitialized) {
        return;
        }

        if (this.cmpRef) {
        this.cmpRef.destroy();
        }

        let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
        this.cmpRef = this.target.createComponent(factory)
        this.cmpRef.instance.data = this.stepinfo;

        this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 22:31









        Daniel W Strimpel

        3,2161416




        3,2161416






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215471%2fhow-to-manually-invoke-these-events-ngoninit-not-getting-fired-for-the-componen%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)