Angular2文档学习的知识点摘要——显示数据、用户输入、表单
目录
显示数据在 Angular 中最典型的数据显示方式,就是把 HTML 模板中的控件绑定到 Angular 组件的属性。 使用插值表达式显示组件属性
用户输入 用户输入触发 DOM 事件。我们通过事件绑定来监听它们,把更新过的数据导入回我们的组件和 model。 模板引用变量还有另一种获取用户数据的方式:使用 Angular 的模板引用变量。 这些变量提供了从模块中直接访问元素的能力。 在标识符前加上井号 (#) 就能声明一个模板引用变量。 @Component({
selector: 'key-up2',template: ` <input #box (keyup)="onKey(box.value)"> <p>{{values}}</p> `
})
export class KeyUpComponent_v2 {
values = '';
onKey(value: string) {
this.values += value + ' | ';
}
}
按键事件过滤
表单模板驱动的表单
13. 在控件上加入模板引用变量,通过 <input type="text" required [(ngModel)]="model.name" name="name" #name="ngModel">
这里,创建了名为name的变量,并且赋值为“ngModel”。 <button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button>
. <button type="submit" [disabled]="!heroForm.form.valid">Submit</button>
案例angular-forms模型类 hero.tsexport class Hero {
constructor(public id: number,public name: string,public power: string,public alterEgo?: string) {
}
}
组件 hero-form.component.tsimport { Component } from '@angular/core';
import { Hero } from './hero';
@Component({
moduleId: module.id,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[0],'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,'','');
}
}
hero-form.component.html<div class="container"> <div [hidden]="submitted"> <h1>Hero Form</h1> <form (ngSubmit)="onSubmit()" #heroForm="ngForm"> <!--{{diagnostic}}--> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" required [(ngModel)]="model.name" name="name" #name="ngModel"> </div> <div [hidden]="name.valid || name.pristine" class="alert alert-danger"> Name is required </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 id="power" class="form-control" required [(ngModel)]="model.power" name="power"> <option *ngFor="let pow of powers" [value]="pow">{{pow}}</option> </select> </div> <button type="submit" class="btn btn-default" [disabled]="!heroForm.form.valid">Submit</button> </form> <br/> <button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button> </div> <div [hidden]="!submitted"> <h2>You submitted the following:</h2> <div class="row"> <div class="col-xs-3">Name</div> <div class="col-xs-9 pull-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-default" (click)="submitted=false">Edit</button> </div> </div>
注:使用到ngModel时,需要在控件上添加name属性。 根模块 app.module.tsimport { 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 { }
注:因为模板驱动的表单位于它们自己的模块,所以在使用表单之前,需要将 根组件 app.component.tsimport { Component } from '@angular/core';
@Component({
selector: 'my-app',template: `<hero-form></hero-form>`,})
export class AppComponent { }
引导根模块 main.tsimport { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
示例页面 index.html在index.html页面的body标签中添加: <my-app>Loading AppComponent content here ...</my-app>
定制CSS类,提供无效控件的视觉反馈 forms.css.ng-valid[required],.ng-valid.required { border-left: 5px solid #42A948; /* green */ }
.ng-invalid:not(form) { border-left: 5px solid #a94442; /* red */ }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- bash – Shell脚本将文件内容与字符串进行比较
- angular – 离子3刷新侧面菜单登录后
- WinForm启用代理服务器调用WebService
- angularjs – 如何使离子列表可滚动?
- 将Application Module中的方法发布为webservice
- 了解GenericTraversableTemplate和其他Scala收集内部
- angularjs – 如何使用Jasmine监视控制器方法?
- 数组 – 在scala中排序ArrayBuffer [A]?
- 网络-Docker Swarm-网络覆盖未连接所有容器
- twitter-bootstrap – 如何在Bootstrap 3中替换FontAwesome