Angular : How to get value in html passing to other component?












2















I'm trying to send value which I'm getting from web service to another component but the problem is that I'm getting empty value in that another component while I can see that the value is present when I do console.log() in the current component.



AppComponent



ts



level: string = '';

getCustomer(id: string) {
this.isLoading = true;

this.customerService.getOne(id)
.subscribe(
(data) => {
this.level = data.level;
console.log(this.level); // Here I can see the value
},
(error) => {
this.errorMessage = error;
},
);
}


html



    <div class="col-lg-8">
<app-other-component [active]="level"></app-other-component>
</div>


AppOtherComponent



ts



@Input() active: string;

ngOnInit() {
console.log(this.active); // Here I'm getting an empty string
}


I think that this line is executing <app-other-component [active]="level"></app-other-component>before the value of 'level' is even filled.



How can I resolve this? thanks.










share|improve this question

























  • Probably because the call to get the data is async and it its not yet present at the time the ngOnInit() is called

    – Teun van der Wijst
    Nov 23 '18 at 9:35






  • 2





    You will have to implement OnChanges to get the updated value

    – Yousef khan
    Nov 23 '18 at 9:37
















2















I'm trying to send value which I'm getting from web service to another component but the problem is that I'm getting empty value in that another component while I can see that the value is present when I do console.log() in the current component.



AppComponent



ts



level: string = '';

getCustomer(id: string) {
this.isLoading = true;

this.customerService.getOne(id)
.subscribe(
(data) => {
this.level = data.level;
console.log(this.level); // Here I can see the value
},
(error) => {
this.errorMessage = error;
},
);
}


html



    <div class="col-lg-8">
<app-other-component [active]="level"></app-other-component>
</div>


AppOtherComponent



ts



@Input() active: string;

ngOnInit() {
console.log(this.active); // Here I'm getting an empty string
}


I think that this line is executing <app-other-component [active]="level"></app-other-component>before the value of 'level' is even filled.



How can I resolve this? thanks.










share|improve this question

























  • Probably because the call to get the data is async and it its not yet present at the time the ngOnInit() is called

    – Teun van der Wijst
    Nov 23 '18 at 9:35






  • 2





    You will have to implement OnChanges to get the updated value

    – Yousef khan
    Nov 23 '18 at 9:37














2












2








2








I'm trying to send value which I'm getting from web service to another component but the problem is that I'm getting empty value in that another component while I can see that the value is present when I do console.log() in the current component.



AppComponent



ts



level: string = '';

getCustomer(id: string) {
this.isLoading = true;

this.customerService.getOne(id)
.subscribe(
(data) => {
this.level = data.level;
console.log(this.level); // Here I can see the value
},
(error) => {
this.errorMessage = error;
},
);
}


html



    <div class="col-lg-8">
<app-other-component [active]="level"></app-other-component>
</div>


AppOtherComponent



ts



@Input() active: string;

ngOnInit() {
console.log(this.active); // Here I'm getting an empty string
}


I think that this line is executing <app-other-component [active]="level"></app-other-component>before the value of 'level' is even filled.



How can I resolve this? thanks.










share|improve this question
















I'm trying to send value which I'm getting from web service to another component but the problem is that I'm getting empty value in that another component while I can see that the value is present when I do console.log() in the current component.



AppComponent



ts



level: string = '';

getCustomer(id: string) {
this.isLoading = true;

this.customerService.getOne(id)
.subscribe(
(data) => {
this.level = data.level;
console.log(this.level); // Here I can see the value
},
(error) => {
this.errorMessage = error;
},
);
}


html



    <div class="col-lg-8">
<app-other-component [active]="level"></app-other-component>
</div>


AppOtherComponent



ts



@Input() active: string;

ngOnInit() {
console.log(this.active); // Here I'm getting an empty string
}


I think that this line is executing <app-other-component [active]="level"></app-other-component>before the value of 'level' is even filled.



How can I resolve this? thanks.







javascript angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:46









Antu

768720




768720










asked Nov 23 '18 at 9:31









JunaidJunaid

164212




164212













  • Probably because the call to get the data is async and it its not yet present at the time the ngOnInit() is called

    – Teun van der Wijst
    Nov 23 '18 at 9:35






  • 2





    You will have to implement OnChanges to get the updated value

    – Yousef khan
    Nov 23 '18 at 9:37



















  • Probably because the call to get the data is async and it its not yet present at the time the ngOnInit() is called

    – Teun van der Wijst
    Nov 23 '18 at 9:35






  • 2





    You will have to implement OnChanges to get the updated value

    – Yousef khan
    Nov 23 '18 at 9:37

















Probably because the call to get the data is async and it its not yet present at the time the ngOnInit() is called

– Teun van der Wijst
Nov 23 '18 at 9:35





Probably because the call to get the data is async and it its not yet present at the time the ngOnInit() is called

– Teun van der Wijst
Nov 23 '18 at 9:35




2




2





You will have to implement OnChanges to get the updated value

– Yousef khan
Nov 23 '18 at 9:37





You will have to implement OnChanges to get the updated value

– Yousef khan
Nov 23 '18 at 9:37












2 Answers
2






active

oldest

votes


















4














Try wrapping the div in an *ngIf, if you don't want the app-other-component to be visible before the value is set from the web service:



<div class="col-lg-8" *ngIf="level">
<app-other-component [active]="level"></app-other-component>
</div>


And yeah as Yousef suggested, you will get the updated @Input value in ngOnChanges and NOT IN ngOnInit. ngOnChanges is the function that gets called on a component every time one of its @Input property changes. So you'll get the updated @Input property in there:



@Input() active: string;

ngOnChanges() {
console.log(this.active); // Here You'll get the updated `active` string.
}





share|improve this answer


























  • But if he need the component rendered when the level is not setted your answer is not quite good.

    – Jacopo Sciampi
    Nov 23 '18 at 9:38






  • 1





    @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

    – SiddAjmera
    Nov 23 '18 at 9:40











  • @SiddAjmera Thanks! it worked perfectly in my situation

    – Junaid
    Nov 23 '18 at 10:05



















0














I guess that is because of 'this' pointer in callback function (which does not point to the component). Change it to 'self' like that



let self = this;
this.customerService.getOne
...
(data) => { self.level = data.level; }





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',
    autoActivateHeartbeat: false,
    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%2f53443923%2fangular-how-to-get-value-in-html-passing-to-other-component%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Try wrapping the div in an *ngIf, if you don't want the app-other-component to be visible before the value is set from the web service:



    <div class="col-lg-8" *ngIf="level">
    <app-other-component [active]="level"></app-other-component>
    </div>


    And yeah as Yousef suggested, you will get the updated @Input value in ngOnChanges and NOT IN ngOnInit. ngOnChanges is the function that gets called on a component every time one of its @Input property changes. So you'll get the updated @Input property in there:



    @Input() active: string;

    ngOnChanges() {
    console.log(this.active); // Here You'll get the updated `active` string.
    }





    share|improve this answer


























    • But if he need the component rendered when the level is not setted your answer is not quite good.

      – Jacopo Sciampi
      Nov 23 '18 at 9:38






    • 1





      @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

      – SiddAjmera
      Nov 23 '18 at 9:40











    • @SiddAjmera Thanks! it worked perfectly in my situation

      – Junaid
      Nov 23 '18 at 10:05
















    4














    Try wrapping the div in an *ngIf, if you don't want the app-other-component to be visible before the value is set from the web service:



    <div class="col-lg-8" *ngIf="level">
    <app-other-component [active]="level"></app-other-component>
    </div>


    And yeah as Yousef suggested, you will get the updated @Input value in ngOnChanges and NOT IN ngOnInit. ngOnChanges is the function that gets called on a component every time one of its @Input property changes. So you'll get the updated @Input property in there:



    @Input() active: string;

    ngOnChanges() {
    console.log(this.active); // Here You'll get the updated `active` string.
    }





    share|improve this answer


























    • But if he need the component rendered when the level is not setted your answer is not quite good.

      – Jacopo Sciampi
      Nov 23 '18 at 9:38






    • 1





      @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

      – SiddAjmera
      Nov 23 '18 at 9:40











    • @SiddAjmera Thanks! it worked perfectly in my situation

      – Junaid
      Nov 23 '18 at 10:05














    4












    4








    4







    Try wrapping the div in an *ngIf, if you don't want the app-other-component to be visible before the value is set from the web service:



    <div class="col-lg-8" *ngIf="level">
    <app-other-component [active]="level"></app-other-component>
    </div>


    And yeah as Yousef suggested, you will get the updated @Input value in ngOnChanges and NOT IN ngOnInit. ngOnChanges is the function that gets called on a component every time one of its @Input property changes. So you'll get the updated @Input property in there:



    @Input() active: string;

    ngOnChanges() {
    console.log(this.active); // Here You'll get the updated `active` string.
    }





    share|improve this answer















    Try wrapping the div in an *ngIf, if you don't want the app-other-component to be visible before the value is set from the web service:



    <div class="col-lg-8" *ngIf="level">
    <app-other-component [active]="level"></app-other-component>
    </div>


    And yeah as Yousef suggested, you will get the updated @Input value in ngOnChanges and NOT IN ngOnInit. ngOnChanges is the function that gets called on a component every time one of its @Input property changes. So you'll get the updated @Input property in there:



    @Input() active: string;

    ngOnChanges() {
    console.log(this.active); // Here You'll get the updated `active` string.
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 9:45

























    answered Nov 23 '18 at 9:35









    SiddAjmeraSiddAjmera

    15.3k31137




    15.3k31137













    • But if he need the component rendered when the level is not setted your answer is not quite good.

      – Jacopo Sciampi
      Nov 23 '18 at 9:38






    • 1





      @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

      – SiddAjmera
      Nov 23 '18 at 9:40











    • @SiddAjmera Thanks! it worked perfectly in my situation

      – Junaid
      Nov 23 '18 at 10:05



















    • But if he need the component rendered when the level is not setted your answer is not quite good.

      – Jacopo Sciampi
      Nov 23 '18 at 9:38






    • 1





      @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

      – SiddAjmera
      Nov 23 '18 at 9:40











    • @SiddAjmera Thanks! it worked perfectly in my situation

      – Junaid
      Nov 23 '18 at 10:05

















    But if he need the component rendered when the level is not setted your answer is not quite good.

    – Jacopo Sciampi
    Nov 23 '18 at 9:38





    But if he need the component rendered when the level is not setted your answer is not quite good.

    – Jacopo Sciampi
    Nov 23 '18 at 9:38




    1




    1





    @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

    – SiddAjmera
    Nov 23 '18 at 9:40





    @JacopoSciampi, I think that's something that he'll have to explicitly specify in the OP. We can't just assume that right? :)

    – SiddAjmera
    Nov 23 '18 at 9:40













    @SiddAjmera Thanks! it worked perfectly in my situation

    – Junaid
    Nov 23 '18 at 10:05





    @SiddAjmera Thanks! it worked perfectly in my situation

    – Junaid
    Nov 23 '18 at 10:05













    0














    I guess that is because of 'this' pointer in callback function (which does not point to the component). Change it to 'self' like that



    let self = this;
    this.customerService.getOne
    ...
    (data) => { self.level = data.level; }





    share|improve this answer




























      0














      I guess that is because of 'this' pointer in callback function (which does not point to the component). Change it to 'self' like that



      let self = this;
      this.customerService.getOne
      ...
      (data) => { self.level = data.level; }





      share|improve this answer


























        0












        0








        0







        I guess that is because of 'this' pointer in callback function (which does not point to the component). Change it to 'self' like that



        let self = this;
        this.customerService.getOne
        ...
        (data) => { self.level = data.level; }





        share|improve this answer













        I guess that is because of 'this' pointer in callback function (which does not point to the component). Change it to 'self' like that



        let self = this;
        this.customerService.getOne
        ...
        (data) => { self.level = data.level; }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 9:39









        AndreyAndrey

        1144




        1144






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443923%2fangular-how-to-get-value-in-html-passing-to-other-component%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

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Fotorealismo