angular - 使用响应式表单从一个服务中读取数据并创建一个列表
问题描述
我用一些带有名称和代码的产品创建了一项服务。我想使用该对象列表并创建一个列表。请帮我!:) 谢谢
产品.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class DateService {
get pdcSol(): { cod: string; denumire: string }[] {
return this._pdcSol;
}
constructor() { }
private _pdcSol = [
{cod: '065095', denumire: 'Pompa de caldura sol apa F1145-8 '},
{cod: '065096', denumire: 'Pompa de caldura sol apa F1145-10 '},
{cod: '065097', denumire: 'Pompa de caldura sol apa F1145-12 '}];
}
home.component.ts
import { DateService } from '../date.service';
export class HomeComponent implements OnInit {
constructor(private pdc: DateService) {}
}
home.component.html
<form [formGroup]="formSol">
<select formControlName="modelPDC">
<option *ngFor="let pdcSol of this.pdc._pdcSol.denumire"
[value]="pdcSol">{{ pdcSol }}</option>
</select>
</form>
解决方案
您可以将服务分配给组件公共属性。
import { DateService } from '../date.service';
export class HomeComponent implements OnInit {
formSol: FormGroup;
dropdownArr:any;
constructor(private pdc: DateService, private formBuilder: FormBuilder) {}
ngOnInit(){
this.dropdownArr = this.pdc.pdcSol;
this.formSol = this.formBuilder.group({
modelPDC: ['']
});
this.formSol.controls['modelPDC'].valueChanges.subscribe(value => {console.log(value);});
}
}
模板:
<form [formGroup]="formSol">
<select formControlName="modelPDC">
<option *ngFor="let pdcSol of this.dropdownArr"
[value]="pdcSol.denumire">{{ pdcSol.denumire }}</option>
</select>
</form>
推荐阅读
- android - 如何在由另一个密钥加密的 AndroidKeyStore 中生成密钥?
- python - 通过 Jenkins 执行我的 Git 上的 python 脚本
- javascript - 如何在 React Native 中将 createStackNavigator 导出为类?
- javascript - 从 XML 加载游戏图块数据并使用 jQuery 渲染新图块
- java - 如何使用gradle eclipse为vscode中的java项目生成.classpath容器条目(gradle缓存中的所有依赖项)?
- javascript - 使用 Electron 在预加载脚本中导入打字稿文件
- go - 在使用 go 模块的 GOPATH 之外的项目中运行 godog 时出现错误“无法编译 testmain 包”
- php - 我如何创建只有一个特殊字符的生成器字符串?
- excel - 过滤器和整行删除问题
- python - 是否有任何方法打印字典,其中包含来自对象的所有信息?