How to display recieved form input data to other component in Angular 6?











up vote
0
down vote

favorite












register.component.html (the form input component)



<div class="card-content">

<form #registerForm="ngForm" (ngSubmit)="onSubmit(Name.value, Email.value)">
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input type="text" name="Name"
#Name
ngModel required>
<label for="Name">Name</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">mail_outline</i>
<input type="text" name="Email"
#Email
ngModel
required
[pattern]="emailPattern">
<label for="Email">Email</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<button class="btn-large btn-submit"
type="submit"
[disabled]='!registerForm.valid'>Start</button>
</div>
</div>

</form>

</div>


register.component.ts
........................................................................................



import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { QuizService } from '../shared/quiz.service';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$';

constructor(private route: Router, private quiz: QuizService) { }

user = {
username: '',
email: ''
};

ngOnInit() {
}

onSubmit(name, email) {
this.user.username = name;
this.user.email = email;

this.route.navigate(['quiz']);
console.log(this.user.username, this.user.email); // log works!!

}

}


quiz.component.html (here i want to display the data which user entered in register component)
.........................................................



<div class="row">
<div class="col s6 offset-s3">
<h3>Welcome to quiz</h3>

<b>Your Name is: </b>
<br>
<b>Your Email is: </b>
</div>
</div>









share|improve this question
























  • Check this - [stackoverflow.com/questions/53202385/…
    – Rahul
    Nov 8 at 9:27















up vote
0
down vote

favorite












register.component.html (the form input component)



<div class="card-content">

<form #registerForm="ngForm" (ngSubmit)="onSubmit(Name.value, Email.value)">
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input type="text" name="Name"
#Name
ngModel required>
<label for="Name">Name</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">mail_outline</i>
<input type="text" name="Email"
#Email
ngModel
required
[pattern]="emailPattern">
<label for="Email">Email</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<button class="btn-large btn-submit"
type="submit"
[disabled]='!registerForm.valid'>Start</button>
</div>
</div>

</form>

</div>


register.component.ts
........................................................................................



import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { QuizService } from '../shared/quiz.service';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$';

constructor(private route: Router, private quiz: QuizService) { }

user = {
username: '',
email: ''
};

ngOnInit() {
}

onSubmit(name, email) {
this.user.username = name;
this.user.email = email;

this.route.navigate(['quiz']);
console.log(this.user.username, this.user.email); // log works!!

}

}


quiz.component.html (here i want to display the data which user entered in register component)
.........................................................



<div class="row">
<div class="col s6 offset-s3">
<h3>Welcome to quiz</h3>

<b>Your Name is: </b>
<br>
<b>Your Email is: </b>
</div>
</div>









share|improve this question
























  • Check this - [stackoverflow.com/questions/53202385/…
    – Rahul
    Nov 8 at 9:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











register.component.html (the form input component)



<div class="card-content">

<form #registerForm="ngForm" (ngSubmit)="onSubmit(Name.value, Email.value)">
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input type="text" name="Name"
#Name
ngModel required>
<label for="Name">Name</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">mail_outline</i>
<input type="text" name="Email"
#Email
ngModel
required
[pattern]="emailPattern">
<label for="Email">Email</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<button class="btn-large btn-submit"
type="submit"
[disabled]='!registerForm.valid'>Start</button>
</div>
</div>

</form>

</div>


register.component.ts
........................................................................................



import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { QuizService } from '../shared/quiz.service';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$';

constructor(private route: Router, private quiz: QuizService) { }

user = {
username: '',
email: ''
};

ngOnInit() {
}

onSubmit(name, email) {
this.user.username = name;
this.user.email = email;

this.route.navigate(['quiz']);
console.log(this.user.username, this.user.email); // log works!!

}

}


quiz.component.html (here i want to display the data which user entered in register component)
.........................................................



<div class="row">
<div class="col s6 offset-s3">
<h3>Welcome to quiz</h3>

<b>Your Name is: </b>
<br>
<b>Your Email is: </b>
</div>
</div>









share|improve this question















register.component.html (the form input component)



<div class="card-content">

<form #registerForm="ngForm" (ngSubmit)="onSubmit(Name.value, Email.value)">
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input type="text" name="Name"
#Name
ngModel required>
<label for="Name">Name</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">mail_outline</i>
<input type="text" name="Email"
#Email
ngModel
required
[pattern]="emailPattern">
<label for="Email">Email</label>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<button class="btn-large btn-submit"
type="submit"
[disabled]='!registerForm.valid'>Start</button>
</div>
</div>

</form>

</div>


register.component.ts
........................................................................................



import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { QuizService } from '../shared/quiz.service';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$';

constructor(private route: Router, private quiz: QuizService) { }

user = {
username: '',
email: ''
};

ngOnInit() {
}

onSubmit(name, email) {
this.user.username = name;
this.user.email = email;

this.route.navigate(['quiz']);
console.log(this.user.username, this.user.email); // log works!!

}

}


quiz.component.html (here i want to display the data which user entered in register component)
.........................................................



<div class="row">
<div class="col s6 offset-s3">
<h3>Welcome to quiz</h3>

<b>Your Name is: </b>
<br>
<b>Your Email is: </b>
</div>
</div>






javascript arrays angular dom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 9:51









Groben

1,0061823




1,0061823










asked Nov 8 at 8:58









TheV

387




387












  • Check this - [stackoverflow.com/questions/53202385/…
    – Rahul
    Nov 8 at 9:27


















  • Check this - [stackoverflow.com/questions/53202385/…
    – Rahul
    Nov 8 at 9:27
















Check this - [stackoverflow.com/questions/53202385/…
– Rahul
Nov 8 at 9:27




Check this - [stackoverflow.com/questions/53202385/…
– Rahul
Nov 8 at 9:27












2 Answers
2






active

oldest

votes

















up vote
2
down vote













this.route.navigate(['quiz',{username:username,...}])


and in your quiz component receive data



this.route.params.subscribe(params=>{ console.log(params.username)})


don't forget inject the ActivatedRoute in your quiz



constructor(private route:ActivatedRoute)





share|improve this answer





















  • Thanks man!! it works :)
    – TheV
    Nov 8 at 12:20


















up vote
1
down vote













There are many way to accomplish this but I would suggest looking at NgRX Store, if you want a simpler solution then you can use a service:



@Injectable({
provideIn: ‘root’
})
export class SomeService {
userSubject = new BehaviorSubject<User>({});
user$: Observable<User> = this.userSubject;
}


In your RegisterComponent submit event you will call someService.userSubject.next(this.user); and in your QuizComponent subscribe to someService.user$ to pick up the data






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%2f53204356%2fhow-to-display-recieved-form-input-data-to-other-component-in-angular-6%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








    up vote
    2
    down vote













    this.route.navigate(['quiz',{username:username,...}])


    and in your quiz component receive data



    this.route.params.subscribe(params=>{ console.log(params.username)})


    don't forget inject the ActivatedRoute in your quiz



    constructor(private route:ActivatedRoute)





    share|improve this answer





















    • Thanks man!! it works :)
      – TheV
      Nov 8 at 12:20















    up vote
    2
    down vote













    this.route.navigate(['quiz',{username:username,...}])


    and in your quiz component receive data



    this.route.params.subscribe(params=>{ console.log(params.username)})


    don't forget inject the ActivatedRoute in your quiz



    constructor(private route:ActivatedRoute)





    share|improve this answer





















    • Thanks man!! it works :)
      – TheV
      Nov 8 at 12:20













    up vote
    2
    down vote










    up vote
    2
    down vote









    this.route.navigate(['quiz',{username:username,...}])


    and in your quiz component receive data



    this.route.params.subscribe(params=>{ console.log(params.username)})


    don't forget inject the ActivatedRoute in your quiz



    constructor(private route:ActivatedRoute)





    share|improve this answer












    this.route.navigate(['quiz',{username:username,...}])


    and in your quiz component receive data



    this.route.params.subscribe(params=>{ console.log(params.username)})


    don't forget inject the ActivatedRoute in your quiz



    constructor(private route:ActivatedRoute)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 9:07









    junk

    211317




    211317












    • Thanks man!! it works :)
      – TheV
      Nov 8 at 12:20


















    • Thanks man!! it works :)
      – TheV
      Nov 8 at 12:20
















    Thanks man!! it works :)
    – TheV
    Nov 8 at 12:20




    Thanks man!! it works :)
    – TheV
    Nov 8 at 12:20












    up vote
    1
    down vote













    There are many way to accomplish this but I would suggest looking at NgRX Store, if you want a simpler solution then you can use a service:



    @Injectable({
    provideIn: ‘root’
    })
    export class SomeService {
    userSubject = new BehaviorSubject<User>({});
    user$: Observable<User> = this.userSubject;
    }


    In your RegisterComponent submit event you will call someService.userSubject.next(this.user); and in your QuizComponent subscribe to someService.user$ to pick up the data






    share|improve this answer

























      up vote
      1
      down vote













      There are many way to accomplish this but I would suggest looking at NgRX Store, if you want a simpler solution then you can use a service:



      @Injectable({
      provideIn: ‘root’
      })
      export class SomeService {
      userSubject = new BehaviorSubject<User>({});
      user$: Observable<User> = this.userSubject;
      }


      In your RegisterComponent submit event you will call someService.userSubject.next(this.user); and in your QuizComponent subscribe to someService.user$ to pick up the data






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        There are many way to accomplish this but I would suggest looking at NgRX Store, if you want a simpler solution then you can use a service:



        @Injectable({
        provideIn: ‘root’
        })
        export class SomeService {
        userSubject = new BehaviorSubject<User>({});
        user$: Observable<User> = this.userSubject;
        }


        In your RegisterComponent submit event you will call someService.userSubject.next(this.user); and in your QuizComponent subscribe to someService.user$ to pick up the data






        share|improve this answer












        There are many way to accomplish this but I would suggest looking at NgRX Store, if you want a simpler solution then you can use a service:



        @Injectable({
        provideIn: ‘root’
        })
        export class SomeService {
        userSubject = new BehaviorSubject<User>({});
        user$: Observable<User> = this.userSubject;
        }


        In your RegisterComponent submit event you will call someService.userSubject.next(this.user); and in your QuizComponent subscribe to someService.user$ to pick up the data







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 9:10









        Neil Stevens

        89811537




        89811537






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53204356%2fhow-to-display-recieved-form-input-data-to-other-component-in-angular-6%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()