When you create a Form in Angular 2, you can easily get all the values from the Form using ControlGroup and Controls.

  • Bind [ng-form-model] to the <form>
  • form bind to ControlGoup
  • Bind [ng-form-control] to the <input>
  • input bind to Gontrol
import {Component, View, FORM_DIRECTIVES, ControlGroup, Control} from 'angular2/angular2';

@Component({
selector: 'field-form'
})
@View({
directives: [FORM_DIRECTIVES],
template: `
<form [ng-form-model]="johnform">
Title: <input type="checkbox" ng-control="title">
Action: <input type="checkbox" ng-control="action">
</form>
`
}) export class FieldForm {
johnform = new ControlGroup({
title: new Control(true),
action: new Control(true)
}); get selectedField(){
     // return ['title', 'action'] 
return Object.keys(this.johnform.controls)
.filter((key)=>{
return this.johnform.controls[key].value;
})
}
}

----------------------

import {Component, View, NgFor, FORM_DIRECTIVES} from 'angular2/angular2';
import {TodoService} from './todoService';
import {TodoItemRender} from './todoItemRender';
import {StartsWith} from './startsWith';
import {SimpleSearch} from './simpleSearch';
import {LetterSelect} from './letterSelect';
import {TodoSearch} from './todoSearch';
import {FieldForm} from './fieldForm'; @Component({
selector: 'todo-list'
})
@View({
pipes: [StartsWith, SimpleSearch],
directives: [NgFor, TodoItemRender, FORM_DIRECTIVES, LetterSelect, TodoSearch, FieldForm],
template: `
<div>
<field-form #field-form></field-form>
<todo-search #todo-search></todo-search>
{{todoSearch.term}}
<todo-item-render
*ng-for="#todo of todoService.todos
| simpleSearch: fieldForm.selectedField : todoSearch.term"
[todoinput]="todo"
>
</todo-item-render>
</div>
{{fieldForm.selectedField | json}}
`
})

[Angular 2] A Simple Form in Angular 2的更多相关文章

  1. [Angular] Implement a custom form component by using control value accessor

    We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...

  2. Angular项目构建指南 - 不再为angular构建而犹豫不决(转)

    如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余 ...

  3. [Angular 2 Router] Configure Your First Angular 2 Route

    Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and ...

  4. 【Angular专题】——(1)Angular,孤傲的变革者

    目录 一. 漫谈Angular 二. 如果你还在使用Angularjs 三. 我计划这样学习Angular技术栈 一. 漫谈Angular Angular,来自Google的前端SPA框架,与Reac ...

  5. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  6. Angular 从入坑到挖坑 - Angular 使用入门

    一.Overview angular 入坑记录的笔记第一篇,完成开发环境的搭建,以及如何通过 angular cli 来创建第一个 angular 应用.入坑一个多星期,通过学习官方文档以及手摸手的按 ...

  7. [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

    前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...

  8. [AngularJS] Angular 1.3 $submitted for Form in Angular

    AngularJS 1.3 add $submitted for form, so you can use  $submitted  to track whether the submit event ...

  9. [Angular2 Form] Angular 2 Template Driven Form Custom Validator

    In this tutorial we are going to learn how we can also implement custom form field validation in Ang ...

随机推荐

  1. 重新开始学习javase_Exception

    “违例”(Exception)这个词表达的是一种“例外”情况,亦即正常情况之外的一种“异常”.在问题发生的时候,我们可能不知具体该如何解决,但肯定知道已不能不顾一切地继续下去.此时,必须坚决地停下来, ...

  2. cocos2dx 3.2中的物理引擎初探(一)

    cocos2dx在设计之初就集成了两套物理引擎,它们是box2d和chipmunk.我目前使用的是最新版的cocos2dx 3.2.引擎中默认使用的是chipmunk,如果想要改使用box2d的话,需 ...

  3. Objective-C内存管理与原理

    尽管苹果在 iOS 5/ Mac OS X 10.7 开始导入ARC,利用 Xcode4.2 可以使用该机能.ARC就是自动引用计数,是一项为Objective - C程序在编译时提供自动内存管理的功 ...

  4. 09_linux下安装Nvidia显卡驱动

    下载驱动 去官网找去,哈哈o(^▽^)o 安装kernel source [root@localhost ~]# yum install kernel-devel 如果还不行,试试下面的 [root@ ...

  5. ftp文件操作

    PHP FTP操作类( 上传.拷贝.移动.删除文件/创建目录 ) 2016-06-01 PHP编程 /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ ...

  6. jQuery获取JSON格式数据方法

    getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ ...

  7. python运维开发之第四天

    一.装饰器 1.器:代表函数,装饰器本质是函数,(装饰器他函数) 2.功能:为其他函数添加附加功能 3.原则: (1)不能修改被装饰函数的源代码 (2)不能修改被装饰函数的调用方式 4.实现装饰器知识 ...

  8. JDBC驱动汇总

    Microsoft SQL Server (6.5, 7, 2000 and 2005) and Sybase (10, 11, 12).   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  9. ASP.NET MVC的Action Filter

    一年前写了一篇短文ASP.NET MVC Action Filters,整理了Action Filter方面的资源,本篇文章详细的描述Action Filter.Action Filter作为一个可以 ...

  10. Jackson将json字符串转换成List<JavaBean>

    Jackson处理一般的JavaBean和Json之间的转换只要使用ObjectMapper 对象的readValue和writeValueAsString两个方法就能实现.但是如果要转换复杂类型Co ...