Angular2 form.value

问题描述 投票:2回答:3

我正在尝试制作一个表单以提交其值,如http://victorsavkin.com/post/108837493941/better-support-for-functional-programming-in中所示:

<form #todoForm [new-control-group]="todo">
  <input control-name="description">
  <input control-name="checked">
  <button (click)="updateTodo(todoForm.value)">Update</button>
</form>

但是updateTodo在被调用时变得不确定。此功能已经实现吗?

更新:

我想我知道如何使其运作http://angularjs.blogspot.no/2015/03/forms-in-angular-2.html

angular
3个回答
2
投票

在当前版本的angular2(alpha 26)中,我无法使这些示例正常工作。看来您现在被迫绑定值和更改事件手册。

这里是您表单的完整TypeScript示例:

import {Component, View} from 'angular2/angular2';
import {formDirectives, FormBuilder, Control, ControlGroup} from 'angular2/forms';


@Component({
    selector: 'todo-app',
    injectables: [FormBuilder]
})
@View({
    directives: [  formDirectives],
    template: `<form [control-group]="todo">
  <input #desc [value]="todo.controls.description.value" (keyup)="setControlValue('description', desc.value)">
  <input #chk [checked]="todo.controls.checked" type="checkbox" (change)="setControlValue('checked', chk.checked)">
  <button (click)="updateTodo($event)">Update</button>
</form>`
})
export class Todo{
    todo:ControlGroup;

    constructor(builder:FormBuilder){
        this.todo = builder.group({
            description: new Control('First todo'),
            checked: new Control(true)
        })
    }

    updateTodo(event){
        event.preventDefault();
        console.log(this.todo.controls.description.value);
        console.log(this.todo.controls.checked.value);
    }

    setControlValue(controlName, value){
        this.todo.controls[controlName].updateValue(value);
        this.todo.controls[controlName].markAsDirty();
    }
}

您当然可以通过将输入字段提取到组件中来清理表单标记:

@Component({
    selector: 'input-text',
    properties: ['control']
})
@View({
    template: `<input #ctrl [value]="control.value" (keyup)="setValue(ctrl.value)">`
})
export class InputText{
    control: Control;
    constructor(){
        this.control = new Control('');
    }
    setValue(value){
        this.control.updateValue(value);
        this.control.markAsDirty();
    }
}

@Component({
    selector: 'input-checkbox',
    properties: ['control']
})
@View({
    template: `<input #ctrl [checked]="control.value" (change)="setValue(ctrl.checked)" type="checkbox">`
})
export class InputCheckbox{
    control: Control;
    constructor(){
        this.control = new Control('');
    }
    setValue(value){
        this.control.updateValue(value);
        this.control.markAsDirty();
    }
}

然后您必须更新Todo类的视图部分

@View({
    directives: [formDirectives, InputText, InputCheckbox],
    template: `<form [control-group]="todo">
    <input-text [control]="todo.controls.description"></input-text>
    <input-checkbox [control]="todo.controls.checked"></input-checkbox>
    <button (click)="updateTodo($event)">Update</button>
</form>`
})

0
投票

您还可以使用为表单设计的NgModel。如果您使用<input [ng-model]="myModel.value" />,它将模型的值绑定到视图,每当您更改模型时,视图将被更新。现在,如果要在视图更改时更新模型,则需要绑定事件,而angular2的绑定为您提供了一种不错的方法:<input [(ng-model)]="myModel.value" />这将确保您的视图和模型大约在两个方式绑定。


0
投票

不包含大写字符。

应该是:

<form #todoform [new-control-group]="todo">
  <input control-name="description">
  <input control-name="checked">
  <button (click)="updateTodo(todoform.value)">Update</button>
</form>
© www.soinside.com 2019 - 2024. All rights reserved.