zoukankan      html  css  js  c++  java
  • Angular2 Form

    1 basic usage

    1 import formDirectives 

    import {formDirectives} from 'angular2/forms';
    

    2 inject directive into View

    directives: [formDirectives]
    

    3 config your form template

        template: `<form #f="form" (submit)="onSubmit(f.value)">
                    <input type="text" ng-control="username" />
                    <input type="text" ng-control="password"/>
                    <button type="submit">submit</button>
                  </form>`,
    

        take care : 1 #f='form' 2 ng-control

          after you submit you will get the result like {username:xx,password:xx}

    4 all the code

    /**
     * Created by Administrator on 2015/7/24.
     */
    import {Component,View} from 'angular2/angular2';
    import {formDirectives} from 'angular2/forms';
    
    @Component({
        selector: 'form-test'
    })
    
    @View({
        template: `<form #f="form" (submit)="onSubmit(f.value)">
                    <input type="text" ng-control="username" />
                    <input type="text" ng-control="password"/>
                    <button type="submit">submit</button>
                  </form>`,
        directives: [formDirectives]
    })
    
    export class FormTest {
        constructor() {
        }
    
        onSubmit(formGroup) {
            console.log(formGroup);
        }
    }
    

      

    2 two-way binding

    use [(ng-model)]='xxxx'

    @View({
        template: `<form #f="form" (submit)="onSubmit(f.value)">
                    <input type="text" ng-control="username" [(ng-model)]="login.userName"/>
                    <input type="text" ng-control="password" [(ng-model)]="login.password"/>
                    <button type="submit">submit</button>
                  </form>`,
        directives: [formDirectives]
    })
    

      

    3 ng-control-group

    @View({
        template: `<form #f="form" (submit)="onSubmit(f.value)">
                    <div ng-control-group="basic">
                        <input type="text" ng-control="username" [(ng-model)]="login.basic.userName"/>
                        <input type="text" ng-control="password" [(ng-model)]="login.basic.password"/>
                    </div>
                    <div ng-control-group="company">
                        <input type="text" ng-control="companyname" [(ng-model)]="login.company.companyName"/>
                    </div>
                    <button type="submit">submit</button>
                   </form>`,
        directives: [formDirectives]
    })
    

      use ng-control-group, you will get the result like 

      

    this.login = {
                basic: {
                    userName: 'Jackey',
                    password: '123'
                },
                company: {
                    companyName: ''
                }
            }
    

      

  • 相关阅读:
    从 QSplitter 中移除 QWidget(使用隐藏与显示,切换十分方便,不要真正销毁)
    Qt虽然自己的源代码里不使用Exception,但也提供了一个QException及其子类QUnhandledException
    细说new与malloc的10点区别
    垃圾回收算法
    服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana
    缓存穿透、缓存击穿与缓存雪崩
    微服务介绍
    分库分表
    Spring Boot、微服务架构和大数据
    Linux基本的操作
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4665674.html
Copyright © 2011-2022 走看看