幽梦林子 发表于 2017-8-11 10:31:54

如何制作Angularjs4.0表单

本帖最后由 幽梦林子 于 2017-8-11 15:24 编辑

如何制作Angularjs4.0表单,具体操作如下:


一、创建 表单Hero 模型类
src/app/hero.ts//文件路径
export class Hero {
constructor(
    public id: number,
    public name: string,
    public power: string,
    public alterEgo?: string
) {}
}

二、创建 表单控制器

1.创建 组件
src/app/hero-form.component.ts //文件路径
import { Component } from '@angular/core';

import { Hero }    from './hero';

@Component({
selector: 'hero-form',
templateUrl: './hero-form.component.html'
})
export class HeroFormComponent {

powers = ['Really Smart', 'Super Flexible',
            'Super Hot', 'Weather Changer'];

model = new Hero(18, 'Dr IQ', this.powers, 'Chuck Overstreet');

submitted = false;

onSubmit() { this.submitted = true; }

// TODO: Remove this when we're done
get diagnostic() { return JSON.stringify(this.model); }
newHero() { this.model = new Hero(42, '', '');}

}

2.导入 组件
src/app/app.module.ts//文件路径
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { AppComponent }from './app.component';
import { HeroFormComponent } from './hero-form.component';
@NgModule({
imports: [
    BrowserModule,
    FormsModule
],
declarations: [
    AppComponent,
    HeroFormComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
3.修改 app.component.ts根组件
src/app/app.component.ts//文件路径
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: ''
})
export class AppComponent { }

三、创建视图,初始 HTML 表单模板
src/app/hero-form.component.html//文件路径
<link rel="stylesheet" href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">   
<style>
//定义css
.ng-valid, .ng-valid.required{
border-left: 5px solid #42A948; /* green */
}
.ng-invalid:not(form){
border-left: 5px solid #a94442; /* red */
}

</style>


<div class="container">
<div ="submitted">
    <h1>Hero Form</h1>
    <form (ngSubmit)="onSubmit()" #heroForm="ngForm">   //ngSubmit表单提交
      <div class="form-group">
      <label for="name">Name</label>
      <input type="text" class="form-control" id="name"
               required
               [(ngModel)]="model.name" name="name"   //ngModel数据双向绑定
               #name="ngModel">
      <div ="name.valid || name.pristine"
             class="alert alert-danger">
          Name is required
      </div>
      </div>
      <div class="form-group">
      <label for="alterEgo">Alter Ego</label>
      <input type="text" class="form-control" id="alterEgo"
               [(ngModel)]="model.alterEgo" name="alterEgo">
      </div>
      <div class="form-group">
      <label for="power">Hero Power</label>
      <select class="form-control" id="power"
                required
                [(ngModel)]="model.power" name="power"
                #power="ngModel">
          <option *ngFor="let pow of powers" ="pow">{{pow}}</option>
      </select>
      <div ="power.valid || power.pristine" class="alert alert-danger">//表单字段验证
          Power is required
      </div>
      </div>
      <button type="submit" class="btn btn-success" ="!heroForm.form.valid">Submit</button>
      <button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button>
    </form>
</div>
<div ="!submitted">
    <h2>You submitted the following:</h2>
    <div class="row">
      <div class="col-xs-3">Name</div>
      <div class="col-xs-9pull-left">{{ model.name }}</div>
    </div>
    <div class="row">
      <div class="col-xs-3">Alter Ego</div>
      <div class="col-xs-9 pull-left">{{ model.alterEgo }}</div>
    </div>
    <div class="row">
      <div class="col-xs-3">Power</div>
      <div class="col-xs-9 pull-left">{{ model.power }}</div>
    </div>
    <br>
    <button class="btn btn-primary" (click)="submitted=false">Edit</button>
</div>
</div>

四、ngModel表单Angular CSS 类来更新控件

页: [1]
查看完整版本: 如何制作Angularjs4.0表单