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_IO

    一,认识IO 通过数据流.序列化和文件系统提供系统输入和输出. 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或是网络连接.类似的,当程序需要 ...

  2. c/c++内存机制(一)(转)

    转自:http://www.cnblogs.com/ComputerG/archive/2012/02/01/2334898.html 一:C语言中的内存机制 在C语言中,内存主要分为如下5个存储区: ...

  3. [C++] namespace相关语法

    本段测试代码包括如下内容: (1) 如何访问namespace中声明的名称:(2) namespace导致的相关冲突:(3) namespace可嵌套:(4) 可以在namespace中使用using ...

  4. MySQL中对varchar类型排序问题

    在数据库表中有一个对varchar类型的数值进行desc排序,很简单的要求吧.可是奇怪的现象出现了表中的数据不会根据从高到底进行排序了瞬间有点泪奔的感觉呀还好经过高手指点啊.所以想和大家分享一下希望下 ...

  5. javascript学习(知识点整理)

    有了这个代码,就可以在定义 中增加更多的控制了 后面会举例关于extjs定义的更多控制     此种方案可以解决定义时需要一些函数调用的情况 函数作用域和声明提前: 即由于js是解释性语言,在执行前会 ...

  6. yii2源码学习笔记

    assets   前端资源文件夹,用于管理css js等前端资源文件等 commands   包含命令行命令,文件为控制器文件 config 应用的配置文件 controllers 控制器文件 mai ...

  7. Thinkphp---练习:数据的增删改查

    利用ThinkPHP连接数据库的增删改查的例题:用到的数据库表名Info表,Nation表 数据显示页面:MainController.class.php中的方法(增删改查全包括--function ...

  8. HTML中插入视频

    最常用的向HTML中插入视频的方法有两种,一种是古老的<object></object>标签,一种是html5中的<video></video>标签. ...

  9. PIL Image 转成 wx.Image、wx.Bitmap

    import wx from PIL import Image def ConvertToWxImage(): pilImage = Image.open('1.png') image = wx.Em ...

  10. 转:视觉中国的NoSQL之路:从MySQL到MongoDB

    起因 视觉中国网站(www.chinavisual.com)是国内最大的创意人群的专业网站.2009年以前,同很多公司一样,我们的CMS和社区产品都构建于PHP+Nginx+MySQL之上:MySQL ...