如何从在Angular中包装的可观察类中访问数组属性?

huangapple go评论66阅读模式
英文:

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:

如何从在Angular中包装的可观察类中访问数组属性?

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&lt;ApplicationConfigurationResponse&gt;();
constructor(private stateService: StateManagementService) {}
                    
ngOnInit():void {
   this.applicationConfigurationResponse$ = 
   this.stateService.getApplicationConfigurations;
}
get settings() {
  return this.applicationConfigurationResponse$.pipe(map(res =&gt; res.settings));
}
}

and in html

&lt;div&gt;
   &lt;select class=&quot;dropdown&quot;&gt;
        &lt;option *ngFor=&quot;let setting of settings | async&quot; [value]=&quot;setting.settingId&quot;&gt;
              {{ setting.name }}
         &lt;/option&gt;
   &lt;/select&gt;  
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月14日 09:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定