angular2表单初体验
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表单初体验的更多相关文章
- jQuery 浮动标签插件,帮助你提升表单用户体验
浮动标签模式(Float Label Pattern)是最新流行的一种表单输入域的内容提示方式,当用户在输入框输入内容的时候,原先占位符的内容向上移动,显示在输入的内容的上面.这里推荐的这款 jQue ...
- angular2 表单的理解
angular2表单分为两种,一种为模板驱动,一种为模型驱动: 个人理解两者的不同 模板驱动依靠H5规则进行验证,在提交表单时进行自定义验证: 模型驱动在加载时候已经加载了所有的验证自定义验证,所以不 ...
- Angular2 表单
1. 说明 表单是Web程序中的重要组成部分,构建良好以及实用的表单必须解决如下几个问题: (1). 如何跟踪及更新表单的数据状态 (2). 如何进行表单验证 (3). 如何显示表单验证信息 Angu ...
- Ajax提交表单初接触
<!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...
- [H5表单]html5自带表单验证体验优化及提示气泡修改
慕课网之前录制的视频,js/jquery各种宽高的理解和应用,最近终于上线了.还有一个html5左侧导航没有上线!最近慕课网系列课程让我录制一个html5表单验证的课程.今天就稍微说一下表单验证!另外 ...
- angular2 表单验证
模版式表单 (1) angular遇到form自动接管,不想自动接管,添加ngNoForm,当标签为div时,但想被表单接管,添加ngForm; (2) ngForm可以被模版本地变量引用,以便在模版 ...
- Angular2 表单验证相关
angular4响应式表单与校验http://blog.csdn.net/xiagh/article/details/78360845?locationNum=10&fps=1 How to ...
- angular2的lazyload初体验
angular2自带了lazyload,就是路由的loadChild,要优化ng2项目必不可少.代码已更新到ng-demo ->https://github.com/chenby/ng2-dem ...
- angular2的管道初体验
ng管道是应用里面比较重要的一个技术,他涉及很多功能 包括排序过滤 废话不说 直接上代码 新建个文件夹吧这个samplepipe.ts放进去 然后 你要做什么 在里面写管道代码 然后在app.modu ...
随机推荐
- js获取100个随机数存入数组
. //js获取100个随机数存入数组 $(function () { var arr = []; ; var str = ""; ) { , ); ) { arr[num] = ...
- sql DATEPART() MONTH() convert() cast() dateadd() DATEDIFF() with(nolock)
DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 DATEPART(datepart,date) date 参数是合法的日期表达式.datepart 参数 ...
- DNS的概念,用途,DNS查询的实现算法
1.DNS的概念,用途 DNS是由解析器以及域名服务器组成的. 域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP地址功能的服务器. DNS ...
- 执行”spark-shell –master yarn –deploy-mode client”,虚拟内存大小溢出,报错
在Hadoop 2.7.2集群下执行如下命令: spark-shell --master yarn --deploy-mode client 爆出下面的错误: org.apache.spark.Sp ...
- Android应用APP脱壳笔记
[TOC] 天下游 模拟定位技术点简析 通过代码分析初步猜测模拟定位用到的几处技术点: 获取了Root权限 通过反射获取 android.os.ServiceManager 对应的函数 getServ ...
- iOS SDK具体解释之NSCopying协议
原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn ...
- Linux下查看硬件信息的方法
用硬件检测程序kuduz探测新硬件:service kudzu start ( or restart) 查看CPU信息:cat /proc/cpuinfo 查看板卡信息:cat /proc/pci 查 ...
- iOS TableView索引字体大小设置
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger ...
- LeetCode459. Repeated Substring Pattern
Description Given a non-empty string check if it can be constructed by taking a substring of it and ...
- A - Bi-shoe and Phi-shoe 欧拉函数
/** 题目:A - Bi-shoe and Phi-shoe 链接:https://vjudge.net/contest/154246#problem/A 题意:每一个数都有一个得分,它的得分就是, ...