绑定角度形式选择选项无效



我正在尝试使用以下代码绑定形式选择类型选项:

fieldGroup: [
{
key: 'TimeOffTypeID',
type: 'select',
className: 'flex-40 padding-10',
templateOptions: {
label: 'نوع مرخصی',
placeholder: 'نوع مرخصی',
required: true,
options: this.getTimeoffType,
valueProp: 'TimeOffTypeID',
labelProp: 'TimeOffTypeName',
},

types$: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);

public get getTimeoffType(): Observable<any[]> {
return this.types$.asObservable();
}

和数据Serrvie

getTimeoffTypes() {
this.base
.get(
Controller.TIMEOFFTYPE,
{
TimeOffTypeID: 0,
},
Actions.SEARCH
)
.subscribe(({ result }) => {
console.log(result)
this.types$.next(result);

})

}

结果有我的数据,但此数据不绑定到表单选择选项

我遇到了同样的问题,选项没有显示。下面突出显示的代码帮助解决了问题;templateOptions:{标签:";性别&";,标签道具:"name";,valueProp:";值">选项:[{"name":"男性";,"值":"男性";},{"name":"女性";,"值":"女性";},{"name":"其他";,"值":"其他";}],注意:我使用的是Formly Material

试试这样的东西:

app.service.ts

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class TypeService {
private types$ = new Subject<any>();
sendTypes(type: any) {
this.types$.next({ data: type });
}
clearTypes() {
this.types$.next();
}
getTypes(): Observable<any[]> {
return this.types$.asObservable();
}
}

应用程序组件.ts

import { Component, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { TypeService } from './_services/index';
@Component({ selector: 'app', templateUrl: 'app.component.html' })
export class AppComponent implements OnDestroy {
types: any[] = [];
subscription: Subscription;
constructor(private typeService: TypeService) {
// subscribe to home component messages
this.subscription = this.typeService.getTypes().subscribe( type => {
if (type) {
this.types.push(type);
} else {
// clear messages when empty message received
this.type = [];
}
});
}
ngOnDestroy() {
// unsubscribe to ensure no memory leaks
this.subscription.unsubscribe();
}
}

最新更新