英文:
How to access an array property from a class wrapped inside observable in angular?
问题
以下是您要翻译的内容:
我试图通过访问包装在可观察对象中的类中的数组属性来填充下拉列表。
我有一个如下的接口:
export interface IApplicationConfigurationResponse
{
settings?: Settings[] ] undefined;
}
export class ApplicationConfigurationResponse
{
settings?: Settings[] ] undefined;
}
export class Settings
{
settingId!:number;
name!:string;
}
state-management.service.ts:
@Injectable
export class StateManagementService
{
private subjectConfigurations = new BehaviourSubject<ApplicationConfigurationResponse>(null);
getApplicationConfigurations(){
return this.subjectSettings.asObservable().pipe(filter(k) => k!=null);
}
set saveApplicationConfiguration(state: ApplicationConfigurationResponse)
{
this.subjectConfigurations.next(state);
}
}
master.component.ts:
@Component({
selector: 'app-master',
..
..
})
export class MasterComponent implements OnInit
{
constructor(private myService: MyService,
private stateService: StateManagementService) {}
ngOnInit():void {
this.myService.getApplicationConfigurationsFromBackend().subscribe((res) ==> {
this.stateService.saveApplicationConfiguration = res;
}
}
header.component.ts:
@Component({
selector: 'app-header',
..
..
})
export class HeaderComponent implements OnInit
{
applicationConfigurationResponse$ : Observable<ApplicationConfigurationResponse>();
constructor(private stateService: StateManagementService) {}
ngOnInit():void {
this.applicationConfigurationResponse$ = this.stateService.getApplicationConfigurations;
}
}
header.component.html:
<div>
<select class="dropdown">
<option *ngFor="let setting of applicationConfigurationResponse$.settings | async" [value] = "setting.settingId">
{{ setting.name }}
</option>
</select>
</div>
但是我在下面的代码行上遇到错误:
[![enter image description here][1]][1]
我无法访问包装在可观察对象中的属性。
如何访问`ApplicationConfigurationResponse`中的`Settings[]`并填充下拉列表?
注意:我已将代码部分保留为原文,未进行翻译。
英文:
I am trying to populate a dropdown by accessing an array property from the class which is wrapped inside an observable.
I have an interface like below :
export interface IApplicationConfigurationResponse
{
settings?: Settings[] ] undefined;
}
export class ApplicationConfigurationResponse
{
settings?: Settings[] ] undefined;
}
export class Settings
{
settingId!:number;
name!:string;
}
state-management.service.ts:
@Injectable
export class StateManagementService
{
private subjectConfigurations = new BehaviourSubject<ApplicationConfigurationResponse>(null);
getApplicationConfigurations(){
return this.subjectSettings.asObservable().pipe(filter(k) => k!=null);
}
set saveApplicationConfiguration(state: ApplicationConfigurationResponse)
{
this.subjectConfigurations.next(state);
}
}
master.component.ts:
@Component({
selector: 'app-master',
..
..
})
export class MasterComponent implements OnInit
{
constructor(private myService: MyService,
private stateService: StateManagementService) {}
ngOnInit():void {
this.myService.getApplicationConfigurationsFromBackend().subscribe((res) ==> {
this.stateService.saveApplicationConfiguration = res;
}
}
header.component.ts:
@Component({
selector: 'app-header',
..
..
})
export class HeaderComponent implements OnInit
{
applicationConfigurationResponse$ : Observable<ApplicationConfigurationResponse>();
constructor(private stateService: StateManagementService) {}
ngOnInit():void {
this.applicationConfigurationResponse$ = this.stateService.getApplicationConfigurations;
}
}
header.component.html:
<div>
<select class="dropdown">
<option *ngFor="let setting of applicationConfigurationResponse$.settings | async" [value] = "setting.settingId">
{{ setting.name }}
</option>
</select>
</div>
But I am getting error on below line:
I am not able to access the property which is wrapped inside an observable.
How can I access the Settings[]
from ApplicationConfigurationResponse
and populate a dropdown?
答案1
得分: 1
你需要访问可观察对象,订阅它,然后使用 Async Pipe 使用 settings 属性,或者你可以像这样做:
export class HeaderComponent implements OnInit {
applicationConfigurationResponse$: Observable<ApplicationConfigurationResponse>;
constructor(private stateService: StateManagementService) {}
ngOnInit(): void {
this.applicationConfigurationResponse$ = this.stateService.getApplicationConfigurations;
}
get settings() {
return this.applicationConfigurationResponse$.pipe(map(res => res.settings));
}
}
在 HTML 中:
<div>
<select class="dropdown">
<option *ngFor="let setting of settings | async" [value]="setting.settingId">
{{ setting.name }}
</option>
</select>
</div>
英文:
You need to access the observable, subscribe to it, then use the settings property using Async Pipe, or you can do something like this
export class HeaderComponent implements OnInit {
applicationConfigurationResponse$ :
Observable<ApplicationConfigurationResponse>();
constructor(private stateService: StateManagementService) {}
ngOnInit():void {
this.applicationConfigurationResponse$ =
this.stateService.getApplicationConfigurations;
}
get settings() {
return this.applicationConfigurationResponse$.pipe(map(res => res.settings));
}
}
and in html
<div>
<select class="dropdown">
<option *ngFor="let setting of settings | async" [value]="setting.settingId">
{{ setting.name }}
</option>
</select>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论