[Angular 2] A Simple Form in Angular 2

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}}
    `
})
复制代码

 

posted @   Zhentiw  阅读(408)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-11-23 [ES6] 15. Generators -- 2
2014-11-23 [ES6] 14. Generator -- 1. yield & next()
2014-11-23 [ES6] 13. Using the ES6 spread operator ...
2014-11-23 [ES6] 12. Shorthand Properties in ES6
点击右上角即可分享
微信分享提示