我在Angular中具有搜索功能。我使用object.assign将参数分配给对象。但是,并非所有字段都是必填字段。如果存在一个空字段,则将其作为一个空字符串发送出去。
如何仅分配具有值的属性。
export class PropertySearchComponent implements OnInit {
searchForm: FormGroup;
searchParams: any = {};
properties: Property;
constructor(private advertService: AdvertService, private alertify: AlertifyService, private formBuilder: FormBuilder) { }
ngOnInit() {
this.createSearchForm();
}
createSearchForm() {
this.searchForm = this.formBuilder.group({
county: ['', Validators.nullValidator],
town: ['', Validators.nullValidator],
min_bedrooms: ['', Validators.nullValidator],
max_bedrooms: ['', Validators.nullValidator],
min_bathrooms: ['', Validators.nullValidator],
max_bathrooms: ['', Validators.nullValidator],
min_price: ['', Validators.nullValidator],
max_price: ['', Validators.nullValidator],
selling_type: ['', Validators.nullValidator],
property_type: ['', Validators.nullValidator],
});
}
search() {
this.searchParams = (Object.assign({}, this.searchForm.value));
this.advertService.propertySearch(this.searchParams).subscribe(data => {
this.properties = data;
this.properties.forEach(property => {
if (property.photos) {
property.mainPhotoUrl = property.photos['url'];
console.log(property.mainPhotoUrl);
}
});
console.log(this.properties);
}, error => {
console.log(error);
});
}
}
慕码人2483693
开满天机
翻过高山走不出你
慕桂英4014372
相关分类