Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, ViewChild } from '@angular/core';
  2. import { NgForm } from '@angular/forms';
  3.  
  4. @Component({
  5.   selector: 'app-root',
  6.   templateUrl: './app.component.html',
  7.   styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent {
  10.  
  11.  
  12.   @ViewChild('f') form: NgForm;
  13.   defaultQuestion = 'pet';
  14.   answer: string = '';
  15.   genders = ['male', 'female', 'other'];
  16.  
  17.   user = {
  18.     username: '',
  19.     email: '',
  20.     secret: '',
  21.     gender: '',
  22.     answer: '',
  23.   };
  24.    
  25.  
  26.  
  27.   submitted: boolean = false;
  28.  
  29.   suggestUserName() {
  30.     const suggestedName = 'Superuser';
  31.     // this.form.setValue({
  32.     //   userData: {
  33.     //     username: suggestedName,
  34.     //     email: ''
  35.     //   },
  36.     //   secret: 'pet',
  37.     //   questionAnswer: '',
  38.     //   gender: 'male'
  39.     // });
  40.     // =============================
  41.     // Set value is for the whole form while patch
  42.     // value is for one aspect of it.
  43.     this.form.form.patchValue({userData: {username:suggestedName}});  
  44.   }
  45.  
  46.   // onSubmit(form: NgForm) {
  47.   //   console.log(form);
  48.   //   console.log("email: " + form.value.email);
  49.   //   console.log("username: " + form.value.username);
  50.   // }
  51.  
  52.   // Another way to access via the viewchild
  53.   onSubmit() {
  54.     this.submitted = true;
  55.     console.log(this.form);
  56.     this.user.username = this.form.value.userData.username;
  57.     console.log(this.user.username);
  58.     // this.user.email = this.form.value.userData.email.value;
  59.     // this.user.secret = this.form.value.secret;
  60.     // this.user.answer = this.form.value.questionAnswer;
  61.     // this.user.gender = this.form.value.gender;
  62.   }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement