angular2学习笔记(7)

Form指令

Form指令需要引入NgForm 组件:import {NgForm} from 'angular2/common'

实例如下: 新建一个student.ts文件,作为实体类定义

export class Student{
  constructor(
    public id: number,
    public name: string,
    public age: number,
    public address?: string
  ) {  }
}

address?表示在new 一个 student对象的时候,这个属性可以忽略

新建一个student-form.component.ts文件

import {Component} from '@angular/core';
import {NgForm}    from '@angular/common';
import { Student}    from './student';
@Component({
  selector: 'student-form',
  templateUrl: 'app/student-form.component.html'
})
export class StudentFormComponent {
  names= ['桂XX', '石XX', '陈XX', '沈XX'];
  model = new Student(1880,  this.names[0], 25,"上海市江月路雷特商务1803号");
  submitted = false;
  onSubmit() { this.submitted = true; }
  // TODO: Remove this when we're done
  get diagnostic() { return JSON.stringify(this.model); }
}

上面的模板是外部引入方式,需要添加一个模板文件 hero-form.component.html

 class="container">
    

Student Form

class="form-group"> for="name">名字 type="text" class="form-control" required>
class="form-group"> for="age">年龄 type="text" class="form-control" required>
class="form-group"> for="address">住址 type="text" class="form-control">
type="submit" class="btn btn-default">Submit

app.component.ts的代码修改一下:

import {Component}         from '@angular/core';
import {StudentFormComponent } from './student-form.component'
@Component({
  selector: 'my-app',
  template: '',
  directives: [StudentFormComponent ]
})
export class AppComponent { }

*ngFor运用到form中

修改上面的form表单 使用*ngFor循环names数组。

 class="form-group">
     for="name">名字
     class="form-control" required>
         *ngFor="#p of names" [value]="p">{{p}}
    

使用*ngModel 数据绑定

 class="form-control" required  [(ngModel)]="model.name" >
...


 type="text" class="form-control" required  [(ngModel)]="model.age" >

用ngControl进行状态跟踪和校验

 class="form-control" required  [(ngModel)]="model.name" ngControl="name">
...


 type="text" class="form-control" required  [(ngModel)]="model.age" ngControl="age">

ngcontrol不只是跟踪状态 ,它通过三种样式类来更新控件 这些样式反应了控件状态。

  • 控件已访问 ---------true:ng-touched ------false:ng-untouched
  • 控件的值已更新-----true:ng-dirty-----------false:ng-pristine
  • 控件的值是有效的---true:ng-valid-----------false:ng-invalid

增加控件对应的样式style.css

.ng-valid[required] {
  border-left: 5px solid #42A948; /* green */
}

.ng-invalid {
  border-left: 5px solid #a94442; /* red */
}

index.html中引入style.css

显示/隐藏校验错误信息

      class="form-group">
         for="age">年龄
         type="text" class="form-control" required  [(ngModel)]="model.age" 
                 ngControl="age"  #age="ngForm">

            [hidden]="age.valid" class="alert alert-danger">请输入您的年龄

提示 :添加这段代码时 需要重新 npm start才能正常显示

ngSubmit 提交表单

 (ngSubmit)="onSubmit()" #studentForm="ngForm">
 type="submit" class="btn btn-default"
        [disabled]="!studentForm.form.valid">Submit

切换2个窗体区域

当表单提交后,我们需要显示一个表单提交后的信息,这个时候需要表单窗体的切换。 在组件中定义一个属性 用于切换表单提交前和提交后的显示

submitted = false;

onSubmit() { this.submitted = true; }

submittedtrue时 隐藏输入表单

 [hidden]="submitted">
    

Student Form

(ngSubmit)="onSubmit()" #studentForm="ngForm">

submittedfalse时 隐藏表单清单信息

 [hidden]="!submitted">
  

您提交的信息:

class="row"> class="col-xs-3">姓名
class="col-xs-9 pull-left">{{ model.name }} class="row"> class="col-xs-3">年龄 class="col-xs-9 pull-left">{{ model.age}} class="row"> class="col-xs-3">地址 class="col-xs-9 pull-left">{{ model.address}}
class="btn btn-default" (click)="submitted=false">编辑

你可能感兴趣的:(Angular2)