[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}}
    `
})
时间: 2024-12-10 17:49:46

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

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

如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余前端框架最大的不同,在于它的核心不再是DOM,而是数据,是model.我们惯用的不管是单纯的jQuery还是MVC的Backbone,它们本质仍是让我们更方便更有条理的操作DOM,但是Angular不是.通过一系列魔术般的手法,它将一切的重心转移到数据上.以开发应用而不是操作节点的方式去开发Web,

[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 then importing the configured RouterModule into your main App Module. Use the Wiki Search as example project. Create a HomeComponent to contain every

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

前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应用. 需求 需求大概是这样的: 开一个新的 Angular 项目,并且一开始选择加入 Router 功能 根组件是 AppComponent ,然后下方有三个子组件分别是 page1 page2 page3 可以在 AppComponent 内点击连结切换到这三个页面 参考文档: 路由与导航 Rou

[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 Angular 2 template driven forms, by creating our own custom validation directive. We are going to see how to write such directive and how its a best prac

[Angular 2] Share a Service Across Angular 2 Components and Modules

Services are used to share data between components. They follow a module pattern that allows you to use the data throughout your application so that your data is consistent and in sync. Create a service.module.ts: import { NgModule} from '@angular/co

angular核心原理解析1:angular自启动过程

angularJS的源代码整体上来说是一个自执行函数,在angularJS加载完成后,就会自动执行了. angular源代码中: angular = window.angular || (window.angular = {}) 定义一个全局的angular空对象. 然后: bindJQuery(); //绑定jQuery publishExternalAPI(angular); //扩展angular对象的方法和属性 jqLite(document).ready(function() { an

[Angular] Bind async requests in your Angular template with the async pipe and the &quot;as&quot; keyword

Angular allows us to conveniently use the async pipe to automatically register to RxJS observables and render the result into the template once the request succeeds. This has some drawbacks, especially if we want to bind the same data in multiple par

[Angular 2] How To Debug An Angular 2 Application - Debugging via Augury or the Console

In this lesson we will learn several ways to debug an Angular 2 application, including by using Augury and the console. This is especially useful in situations (typically in enterprise development) where we are not administrators on our machine and d

angular源码分析:图解angular的启动流程

今天做了一些图来说明angular,由于angular实在太复杂了,不知道用什么图表示比较好,所以就胡乱画了一些,希望有人能看得懂. 一.源码文件编译合并顺序图 二.angular.module函数功能图 三.publishExternalAPI函数功能图 四.注入器工厂函数createInjector内部的providerCache和instanceCache