1.创建movie模型。

最近对angular2比较感兴趣,跟着官网学习一段,练习了一个表单demo!

src/app/movie.ts文件:

export class Movie{

    constructor(
public id:number,
public name:string,
public age : string,
public hobby?:string ){}
}

2.创建表单组件。

src/app/movie-form.component.ts文件:

import {Component} from '@angular/core';
import { Movie } from './movie'; @Component({
moduleId:module.id,
selector:'movie-form',
templateUrl:'./movie-form.component.html'
}) export class MovieFormComponent{
introduce = ['小城之春', '音乐之城',
'疯狂原始人', '里约大冒险']; model = new Movie(18,'July yu',this.introduce[0],'惊天魔盗团故事'); submitted = false; onSubmit(){ this.submitted = true; } get disgnostic(){
return JSON.stringify(this.model);
}
newMovie() {
this.model = new Movie(42, '', '');
}
}

3.定义应用的根模块。

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 { MovieFormComponent } from './movie-form.component'; @NgModule({
imports: [ BrowserModule , FormsModule],
declarations: [ AppComponent , MovieFormComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }

4.创建根组件。

src/app/app.component.ts文件:

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

@Component({
selector: 'my-app',
template: '<movie-form></movie-form>'
}) export class AppComponent { }

5.创建表单模板。

src/app/movie-form.component.html文件:

<div class="container">
<div [hidden]="submitted">
<h1>Movie Form</h1>
<form (ngSubmit)="onSubmit()" #movieForm="ngForm">
<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 [hidden]="name.valid || name.pristine" class="alert alert-danger">
Name is required
</div>
</div>
<div class="form-group">
<label for="hobby">Alter hobby</label>
<input type="text" class="form-control" id="hobby" [(ngModel)]="model.hobby" name="hobby">
</div>
<div class="form-group">
<label for="intro">Movie introduce</label>
<select class="form-control" id="intro" required [(ngModel)]="model.introduce" name="intro" #intro="ngModel">
<option *ngFor="let i of introduce" [value]="i">{{i}}</option>
</select>
<div [hidden]="intro.valid || intro.pristine" class="alert alert-danger">
Power is required
</div>
</div>
<button type="submit" class="btn btn-success" [disabled]="!movieForm.form.valid">Submit</button>
<button type="button" class="btn btn-default" (click)="newMovie(); movieForm.reset()">New Movie</button>
</form>
</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 hobby</div>
<div class="col-xs-9 pull-left">{{ model.hobby }}</div>
</div>
<div class="row">
<div class="col-xs-3">introduce</div>
<div class="col-xs-9 pull-left">{{ model.introduce }}</div>
</div>
<br>
<button class="btn btn-primary" (click)="submitted=false">Edit</button>
</div>
</div>

angular2表单初体验的更多相关文章

  1. jQuery 浮动标签插件,帮助你提升表单用户体验

    浮动标签模式(Float Label Pattern)是最新流行的一种表单输入域的内容提示方式,当用户在输入框输入内容的时候,原先占位符的内容向上移动,显示在输入的内容的上面.这里推荐的这款 jQue ...

  2. angular2 表单的理解

    angular2表单分为两种,一种为模板驱动,一种为模型驱动: 个人理解两者的不同 模板驱动依靠H5规则进行验证,在提交表单时进行自定义验证: 模型驱动在加载时候已经加载了所有的验证自定义验证,所以不 ...

  3. Angular2 表单

    1. 说明 表单是Web程序中的重要组成部分,构建良好以及实用的表单必须解决如下几个问题: (1). 如何跟踪及更新表单的数据状态 (2). 如何进行表单验证 (3). 如何显示表单验证信息 Angu ...

  4. Ajax提交表单初接触

    <!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...

  5. [H5表单]html5自带表单验证体验优化及提示气泡修改

    慕课网之前录制的视频,js/jquery各种宽高的理解和应用,最近终于上线了.还有一个html5左侧导航没有上线!最近慕课网系列课程让我录制一个html5表单验证的课程.今天就稍微说一下表单验证!另外 ...

  6. angular2 表单验证

    模版式表单 (1) angular遇到form自动接管,不想自动接管,添加ngNoForm,当标签为div时,但想被表单接管,添加ngForm; (2) ngForm可以被模版本地变量引用,以便在模版 ...

  7. Angular2 表单验证相关

    angular4响应式表单与校验http://blog.csdn.net/xiagh/article/details/78360845?locationNum=10&fps=1 How to ...

  8. angular2的lazyload初体验

    angular2自带了lazyload,就是路由的loadChild,要优化ng2项目必不可少.代码已更新到ng-demo ->https://github.com/chenby/ng2-dem ...

  9. angular2的管道初体验

    ng管道是应用里面比较重要的一个技术,他涉及很多功能 包括排序过滤 废话不说 直接上代码 新建个文件夹吧这个samplepipe.ts放进去 然后 你要做什么 在里面写管道代码 然后在app.modu ...

随机推荐

  1. php中的可变变量、可变函数、匿名函数

    1.可变变量 简而言之:获取一个普通变量的值作为这个可变变量的变量名. 如: $a = "hello"; $$a = " world"; /* $a 的值为&q ...

  2. linux tomacat 之部署 war包

    之前一篇写的是tomcat的linux安装,其中主要是jre 的问题,接下来讲讲 tomcat发布war包 这是一个相对简单的过程 打好war包后,将war包上传到/usr/local/tomcat/ ...

  3. TFTP 与 FTP的区别

    FTP(File Transfer Protocol,文件传输协议)协议在TCP/IP协议族中属于应用层协议,用于在远端服务器和本地客户端之间传输文件,使用TCP端口20和21进行传输.端口20用于传 ...

  4. Sql中的内连接,左连接以及右连接区别

    转自:http://pangaoyuan.javaeye.com/blog/713177 有两个表A和表B. 表A结构如下: Aid:int:标识种子,主键,自增ID Aname:varchar 数据 ...

  5. JQuery常用操作实现方式

    http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...

  6. C# AppDomain 类

    /*** AppDomain 表示应用程序域,它是一个应用程序在其中执行的独立环境.无法继承此类. 应用程序域(由 AppDomain 对象表示)为执行托管代码提供隔离.卸载和安全边界. 使用应用程序 ...

  7. TP中单字母快捷函数总结

    ThinkPHP中有许多使用简便的单字母函数,可以很方便开发者快速的调用,但是字母函数却不方便记忆,本文将所有的字母函数总结一下,以方便以后查找.1.U() URL组装 支持不同URL模式 U($ur ...

  8. java 读取world的图片 并把图片路径存入数据库

    package World; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcep ...

  9. Cobbler简介

    Cobbler is a Linux provisioning server that facilitates and automates the network-based system insta ...

  10. printf,sprintf,vsprintf

    printf,sprintf比较常用,vsprintf不常用. 1. 三个函数的声明: int printf (const char * szFormat, ...); int sprintf (ch ...