简体   繁体   English

Angular2 表单值

[英]Angular2 form.value

I'm trying to make a form to submit its value like shown in http://victorsavkin.com/post/108837493941/better-support-for-functional-programming-in :我正在尝试制作一个表单来提交其值,如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>

but updateTodo gets undefine when invoked.但是updateTodo在调用时未定义。 Has this functionality been already implemented?这个功能已经实现了吗?

UPDATE:更新:

I think I know how to make it working http://angularjs.blogspot.no/2015/03/forms-in-angular-2.html我想我知道如何让它工作http://angularjs.blogspot.no/2015/03/forms-in-angular-2.html

In the current version of angular2 (alpha 26), I'm not able to make those samples work.在 angular2 (alpha 26) 的当前版本中,我无法使这些示例工作。 It seems like you for now are forced to bind value and change events manual.看起来您现在被迫手动绑定值和更改事件。

Here is a complete TypeScript sample of your form:这是您表单的完整 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();
    }
}

You could of course clean up the form markup by extracting the input fields into compoenents:您当然可以通过将输入字段提取到组件中来清理表单标记:

@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();
    }
}

And then you would have to update the view part of your Todo class然后你将不得不更新你的 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>`
})

You can also use NgModel which is designed for forms.您还可以使用专为表单设计的 NgModel。 If you use <input [ng-model]="myModel.value" /> , it'll bind the value of the model to the view, whenever you change your model the view will be updated.如果您使用<input [ng-model]="myModel.value" /> ,它会将模型的值绑定到视图,每当您更改模型时,视图都会更新。 Now, if you want to update your model when the view gets to change, you need to bind the event, and the binding of angular2 gives you a nice way to do this : <input [(ng-model)]="myModel.value" /> this will guarantee that your view and model around two-way bound.现在,如果你想在视图改变时更新你的模型,你需要绑定事件,angular2 的绑定为你提供了一个很好的方法: <input [(ng-model)]="myModel.value" />这将保证您的视图和模型围绕双向绑定。

Not contain uppercase character.不包含大写字符。

It should be:它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM