gpt4 book ai didi

angular - 选择表单控件在 Angular 7 中返回 [object Object]

转载 作者:行者123 更新时间:2023-12-03 23:53:14 26 4
gpt4 key购买 nike

在我的 Angular 7 项目中,我有一个 react 形式 select*ngFor块。 ngFor 值根据从选项中选择的值进行过滤,自定义管道负责过滤。每当我从选项中选择一个值时,我看到的只有 "[object Object]"输出。我试过 (ngModelChange)=fun() , change事件。他们没有工作。
表格 :

    <div class="container container-fluid">
<h3 style="text-align: center"> BCMC CyberSecurity Jobs</h3>
<form [formGroup]="jobsForm">
<div class="row" style="margin-top: 40px">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<label> Job Type:
<select class="custom-select" formControlName="jobTypeControl" (ngModelChange)="updateFilterValue($event)">
<option *ngFor="let jobType of jobTypeObservable" [value]="jobType"> {{jobType.name}}</option>
</select>
</label>
</div>

<div *ngIf="jobsDataAvailable()" class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<div class="has-text-centered">
<pagination-controls (pageChange)="page = $event" class="my-pagination" directionLinks="true" maxSize="5"
nextLabel="" previousLabel=""></pagination-controls>
</div>
</div>
</div>

<div *ngIf="jobsDataAvailable()">
<div *ngFor="let job of (jobsObservable) | stringFilter: this.jobsForm.controls['jobTypeControl'].value | paginate: { itemsPerPage: 10, currentPage: page }" class="row" style="margin-top: 10px">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{job.title}}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{job.city}}, {{job.state}}</h6>
<p class="card-text">This text will be replaced by clearance value in future</p>
<a class="btn btn-primary" href="https://bcmcllc.applytojob.com/apply/{{job.board_code}}">View and Apply</a>
</div>
</div>
</div>
</div>
</div> <!-- End of ngIf-->
</form>

</div>
自定义过滤器 :
     import {Pipe, PipeTransform} from "@angular/core";
@Pipe({
name: 'stringFilter'
})
export class StringFilterPipe implements PipeTransform
{
transform(value: string[], filterValue: any)
{
if (!filterValue || filterValue === '')
{
return value;
}
return value.filter(item => -1 < item.toLowerCase().indexOf(filterValue.toLowerCase()));
}
}
**JobsListComponent**
    import {Component, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {JobsService} from './services/jobs.service';
import {SERVER_API_URL} from '../../app.constants';
import {Observable} from 'rxjs';
import {Job} from './model/job';
import {FormControl, FormGroup} from '@angular/forms';
import {JobType} from './model/job-type';

@Component({
selector: 'app-jobs-list',
templateUrl: './jobs-list.component.html',
styleUrls: ['./jobs-list.component.css']
})

export class JobsListComponent implements OnInit
{
jobsObservable: Observable<Job[]>;
jobTypeObservable: Observable<JobType[]>;
page: number = 1;

jobsForm = new FormGroup({
jobTypeControl: new FormControl({value: '', disabled: false})
});

//this.form.controls['jobTypeControl'].value
//jobsForm.get('jobTypeControl').value
constructor(private jobsService: JobsService) {
}

ngOnInit()
{
this.getAllJobTypes();
this.getAllJobs();
}

getAllJobs() {
let url = SERVER_API_URL + 'jobs/all';

this.jobsService.getAllJobs(url).subscribe(
data => {
// @ts-ignore
this.jobsObservable = data;
},
error => console.error('Failed to retrieve values from backend, error=> ' + error),
() => console.log('Jobs retrieved from backend'));
return this.jobsObservable;
}

getAllJobTypes() {
let url = SERVER_API_URL + 'jobtypes/all';

this.jobsService.getAllJobTypes(url).subscribe(
data => {
// @ts-ignore
this.jobTypeObservable = data;
},
error => console.error('Failed to retrieve values from backend, error=> ' + error),
() => console.log('Job Types retrieved from backend'));
return this.jobTypeObservable;
}

jobsDataAvailable() {
return this.jobsObservable !== undefined;
}


updateFilterValue(event)
{
console.log("Emitted Value "+event);
}
}

最佳答案

两件事情。
第一:在你的选择中,你有

    <option *ngFor="let jobType of jobTypeObservable" [value]="jobType"> 
{{jobType.name}}
</option>
因此,该值是 jobType 类型的对象。你可以使用,例如。如果您的 jobType 有一个名为“id”的属性 [value]=jobType.id
    <option *ngFor="let jobType of jobTypeObservable" [value]="jobType.id"> 
{{jobType.name}}
</option>
因此,您在函数中收到了“id”,而不是整个对象。
第二:在响应式(Reactive)表单中,通常您订阅值更改 - 它没有被使用(更改) - 然后,在创建表单之后
    jobsForm.get('jobTypeControl').valueChanges.subscribe(value=>
{ //here you has the value of jobTypeControl
console.log(value)
}
)

关于angular - 选择表单控件在 Angular 7 中返回 [object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54223732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com