import { Component }       from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h1>消失的欄位資料</h1>
      <p>點擊下方按鈕並打開F12開發者工具</p>
      <form #f="ngForm">

        <div ngModelGroup="fullName" #nameCtrl="ngModelGroup">
          <input name="first" [ngModel]="fullName.first">
          <input name="last" [ngModel]="fullName.last">
        </div>
        
        <div>
          <button (click)="onSubmit(f,nameCtrl)">Submit</button>
          <button (click)="setValue()">Set value</button>
          <button (click)="disable(f)">Disable</button>
          <button (click)="enable(f)">Enable</button>
        </div>
        
      </form>
    </div>
    
    <div>
    <h2>#nameCtrl="ngModelGroup"</h2>
      <pre>
        {{   nameCtrl.value | json}}
      </pre>
    </div>
    
    <div>
      <h2>#f="ngForm"</h2>
      <pre>
        {{f.value | json}}
      </pre>
    </div>
    
    <div>
      <h2>Use getRawValue()</h2>
      {{ nameCtrl.control?.getRawValue() | json }}
    </div>
  `
})
export class AppComponent {
  fullName = {first: 'Jeff', last: 'Wu'};
  
  constructor() {}
  
  onSubmit(f: NgForm, nameCtrl) {
    console.log(f);
    console.log(f.value);  // {name: {first: 'Jeff', last: 'Wu'}, email: ''}
    console.log(nameCtrl);
  }
  
  setValue() { this.fullName = {first: 'Kevin', last: 'Yang'}; }
  
  disable(f:NgForm) {
    f.form.controls.fullName.controls.first.disable();
    console.log(f.form.controls.fullName.controls.first);
    console.log('after disable:', f);
  }
  
  enable(f:NgForm) {
    f.form.controls.fullName.controls.first.enable();
    console.log('after enable:', f);
  }
}


/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { BrowserModule }                from '@angular/platform-browser';
import { FormsModule }          from '@angular/forms';
import { NgModule }                     from '@angular/core';

import { AppComponent }                 from './app.component';


@NgModule({
  imports: [ BrowserModule, FormsModule ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
  constructor() {
  }
}


/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);


/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
.errorMessage{
  color:red;
}

.form-row{
  margin-top: 10px;
}

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
/* Master Styles */
h1 {
  color: #369;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 250%;
}
h2, h3 {
  color: #444;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: lighter;
}
body {
  margin: 2em;
}
body, input[text], button {
  color: #888;
  font-family: Cambria, Georgia;
}
a {
  cursor: pointer;
  cursor: hand;
}
button {
  font-family: Arial;
  background-color: #eee;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  cursor: hand;
}
button:hover {
  background-color: #cfd8dc;
}
button:disabled {
  background-color: #eee;
  color: #aaa;
  cursor: auto;
}

/* Navigation link styles */
nav a {
  padding: 5px 10px;
  text-decoration: none;
  margin-right: 10px;
  margin-top: 10px;
  display: inline-block;
  background-color: #eee;
  border-radius: 4px;
}
nav a:visited, a:link {
  color: #607D8B;
}
nav a:hover {
  color: #039be5;
  background-color: #CFD8DC;
}
nav a.active {
  color: #039be5;
}

/* items class */
.items {
  margin: 0 0 2em 0;
  list-style-type: none;
  padding: 0;
  width: 24em;
}
.items li {
  cursor: pointer;
  position: relative;
  left: 0;
  background-color: #EEE;
  margin: .5em;
  padding: .3em 0;
  height: 1.6em;
  border-radius: 4px;
}
.items li:hover {
  color: #607D8B;
  background-color: #DDD;
  left: .1em;
}
.items li.selected {
  background-color: #CFD8DC;
  color: white;
}
.items li.selected:hover {
  background-color: #BBD8DC;
}
.items .text {
  position: relative;
  top: -3px;
}
.items .badge {
  display: inline-block;
  font-size: small;
  color: white;
  padding: 0.8em 0.7em 0 0.7em;
  background-color: #607D8B;
  line-height: 1em;
  position: relative;
  left: -1px;
  top: -4px;
  height: 1.8em;
  margin-right: .8em;
  border-radius: 4px 0 0 4px;
}
/* everywhere else */
* {
  font-family: Arial, Helvetica, sans-serif;
}


/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8" />
    <script>document.write('<base href="' + document.location + '" />');</script>
    <title>Dynamic Form</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="styles.css" />
    <link rel="stylesheet" href="sample.css" />
    
    <!-- Polyfills for older browsers -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.8"></script>
    <script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
    <script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading app...</my-app>
    <!-- 
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
  </body>

</html>