在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
描述在本章中,我们将学习如何创建表单。 我们将在我们的示例中使用以下类和指令。
例子下面的例子描述了如何在Angular 2中创建一个表单: <html> <head> <title>Contact Form</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank" > <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/es6-shim.min.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/system-polyfills.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/angular2-polyfills.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/system.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/typescript.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/Rx.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: {'app': {defaultExtension: 'ts'}} }); System.import('/angular2/src/app/main') .then(null, console.error.bind(console)); </script> </head> <body> <my-app>Loading...</my-app> </body> </html> 上述代码包括以下配置选项:
要运行代码,您需要以下TypeScript(.ts)文件,您需要保存在应用程序文件夹下。 form_main.tsimport {bootstrap} from 'angular2/platform/browser'; import {AppComponent} from "./data_binding_app.component"; bootstrap(AppComponent);contact.ts export class Contact { constructor( public firstname: string, public lastname: string, public country: string, public phone: number ) { } } Contact类包含在我们的表单中使用的名字,姓氏,国家和电话。 forms_app.component.tsimport {Component} from 'angular2/core'; import {ContactComponent} from './contact-form.component' @Component({ selector: 'my-app', template: '', directives: [ContactComponent] }) export class AppComponent { }
import {Component} from 'angular2/core'; import {NgForm} from 'angular2/common'; import { Contact } from './contact'; @Component({ selector: 'contact-form', templateUrl: 'app/contact-form.component.html' }) export class ContactComponent { countries = ['India', 'Australia', 'England', 'South Africa', 'USA', 'Japan', 'Singapore']; contact = new Contact('Ravi', 'Shankar', this.countries[0], 6445874544); submitted = false; onSubmit() { this.submitted = true; } active = true; newContact() { this.contact = new Contact('', ''); this.active = false; setTimeout(()=> this.active=true, 0); } }
<div class="container"> <div [hidden]="submitted"> <h2>Contact Form</h2> <form *ngIf="active" (ngSubmit)="onSubmit()" #contactForm="ngForm" novalidate> <div class="form-group"> <label for="firstname">First Name</label> <input type="text" class="form-control" placeholder="Enter Your First Name" required [(ngModel)]="contact.firstname" ngControl="firstname" #firstname="ngForm" > <div [hidden]="firstname.valid || firstname.pristine" class="alert alert-danger"> firstname is required </div> </div> <div class="form-group"> <label for="lastname">Last Name</label> <input type="text" class="form-control" placeholder="Enter Your Last Name" [(ngModel)]="contact.lastname" ngControl="lastname" > </div> <div class="form-group"> <label for="country">Country</label> <select class="form-control" required [(ngModel)]="contact.country" ngControl="country" #country="ngForm" > <option value="" selected disabled>Select Your Country</option> <option *ngFor="#coun of countries" [value]="coun">{{coun}}</option> </select> <div [hidden]="country.valid || country.pristine" class="alert alert-danger"> Country is required </div> </div> <div class="form-group"> <label for="phone">Phone Number</label> <input type="number" class="form-control" placeholder="Enter Your Phone Number" [(ngModel)]="contact.phone" ngControl="phone" > </div> <button type="submit" class="btn btn-primary" [disabled]="!contactForm.form.valid">Submit</button> <button type="button" class="btn btn-primary" (click)="newContact()">New Contact</button> </form> </div> <div [hidden]="!submitted"> <h2>Your contact details :</h2> <div class="well"> <div class="row"> <div class="col-xs-2">First Name</div> <div class="col-xs-10 pull-left">{{ contact.firstname }}</div> </div> <div class="row"> <div class="col-xs-2">Last Name</div> <div class="col-xs-10 pull-left">{{ contact.lastname }}</div> </div> <div class="row"> <div class="col-xs-2">Country</div> <div class="col-xs-10 pull-left">{{ contact.country }}</div> </div> <div class="row"> <div class="col-xs-2">Phone Number</div> <div class="col-xs-10 pull-left">{{ contact.phone }}</div> </div> <br> <button class="btn btn-primary" (click)="submitted=false">Edit Contact</button> </div> </div> </div>
输出让我们执行以下步骤,看看上面的代码如何工作:
或者你可以用另一种方式运行这个文件:
相关文章 |
请发表评论