如何POST复选框值而不是布尔值 - Angular 5

时间:2018-04-05 20:57:25

标签: html angular typescript http angular5

我有这种模板驱动的形式:

<form #form="ngForm" (submit)="submitForm()" novalidate>
    <div class="form-group" [class.has-error]="linkName.invalid">
        <label class="control-label" for="name">Name</label>
        <input #linkName="ngModel" type="text" class="form-control" name="name" id="name" [(ngModel)]="finalData.name" required />
        <div *ngIf="linkName.invalid" class="alert alert-danger">Name is required</div>
    </div>
    <div class="form-group">
        <label for="url">Url</label>
        <input type="text" class="form-control" id="url" name="url" [(ngModel)]="finalData.url" readonly/>
    </div>
    <div class="checkbox" *ngFor="let tag of finalData.tags; index as i" [attr.data-index]="i">
        <label><input type="checkbox" name="{{ 'checkbox' + tag }}" id="{{ 'checkbox' + tag }}" (ngModel)="finalData.tags[i]"/>{{ tag }}</label>
    </div>         
    <button type="submit" [disabled]="form.invalid" class="btn btn-primary btn-success">Save</button>
</form>

我希望POST根据checkboxes的值来确定是否已检查。 这种方式使用单向绑定(ngModel),无论是否检查,它都会POST全部tags。如果我使用双向绑定[(ngModel)],它将POST布尔值,它将根据布尔值更改nameid,这是我不想要的。

实施例: 如果我检查这个,我想要POST foo而不是true

<label><input type="checkbox" name="footag" id="footag" value="foo" [(ngModel)]="finalData.tags[i]"/>foo</label>

这是用于数据绑定的组件:

import { Component, OnInit, Injectable } from "@angular/core";
import { DataService } from "../shared/dataService";
import { FinalFormData } from "../shared/finalFormData";
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from "rxjs/Observable";
import { NgForm } from "@angular/forms";

@Component({
    selector: "final-form",
    templateUrl: "finalForm.component.html"
})

export class FinalFormComponent implements OnInit{

    constructor(private data: DataService, private route: ActivatedRoute, private router: Router) {
    }
    public finalData: FinalFormData;
    public errorMessage = "";
    public isBusy = true;

    submitForm() {
        this.data.postData(this.finalData)
            .subscribe(data => {
                this.finalData = data;
                this.router.navigate(["/"]);
            }, err => console.log(err));
    }

    ngOnInit(): void {
        //let url = encodeURIComponent(this.route.snapshot.queryParams['url']);
        //this.data.loadData(url)
        //    .subscribe(finalData => {
        //        this.finalData = finalData;
        //    }); 
        this.finalData = this.route.snapshot.data['finalData'];
    }
}

FinalFormData类:

export class FinalFormData {
    name: string;
    url: string;
    dateCreated: Date;
    tags: any[];
}

1 个答案:

答案 0 :(得分:3)

您可以使用ngModelChange方法传递值并将其设置为您想要的任何变量。请参阅下面的示例代码段:

<label><input type="checkbox" name="footag" id="footag" value="foo" [ngModel]="finalData.tags[i]" (ngModelChange)="CheckBoxValueChange('foo',finalData)"/>foo</label>

在component.ts文件中,您可以访问该值并将其设置为您想要的任何变量:

CheckBoxValueChange(checkboxValue:string,finalData:any){
    finalData.checkboxText=checkboxValue;
    alert(checkboxValue);
}