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. java之super关键字

    super关键字: 在java中使用super来引用基类的成分. 程序code: class FatherClass{ public int value; public void f(){ value ...

  2. Jenkins部署java项目实例

    源码管理 选择 “git”, Repository URL填写你的git地址,并配置对应的key,大家最好事前先在jenkins这台机器上能够直接远程git操作私有仓库 构建触发器.构建环境.Pre ...

  3. spring中反射机制和注入的使用

    http://www.cnblogs.com/andin/archive/2011/04/30/spring.html spring的一大核心概念是注入, 但是,这存在的一个前提就是类是由spring ...

  4. MVC中上传文件大小限制的解决办法

    在Web.Config文件中配置限制上传文件大小与时间. 需要在配置文件里面设置文件上传限定的两个属性值:maxAllowedContentLength,maxRequestLength 允许上传文件 ...

  5. go项目布局(摘录)

    go的项目结构布局 或 包结构布局 这一块大家似乎还在摸索吧, 常用的应该还是类似于java的mvc布局, 但网上也有不同的布局方式,查阅github上的一些源码,也有大量的采用. 我把自己碰到的资料 ...

  6. SQL Server事务详解

    事务定义: 事务是单个的工作单元.如果某一事务成功,则在该事务中进行的所有数据更改均会提交,成为数据库中的永久组成部分.如果事务遇到错误且必须取消或回滚,则所有数据更改均被清除. 事务三种运行模式:  ...

  7. Atitit  hre框架v5 新特性  HREv5

    Atitit  hre框架v5 新特性  HREv5 1. V5新特性 apiurl2="/wrmiServlet";1 2. V1 新特性1 3. V2 新特性 添加php版1 ...

  8. wxPython 4.0.0b2安装

    https://www.cnblogs.com/NanShan2016/p/5518235.html 亮的界面是一个GUI程序必不可少的一部分,wxPython可以做到这一点,加之Python强大的功 ...

  9. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  10. RabbitMQ(一):Windows下RabbitMQ安装

    1.Windows下安装RabbitMQ需要以下几个步骤 (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang. ...