首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定角度格式选择选项不起作用

绑定角度格式选择选项不起作用
EN

Stack Overflow用户
提问于 2020-09-10 17:18:56
回答 2查看 420关注 0票数 1

我正在尝试使用下面的代码绑定正式的select类型选项:

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

代码语言:javascript
复制
    types$: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);
     
    public get getTimeoffType(): Observable<any[]> {

    return this.types$.asObservable();
    }

和数据服务

代码语言:javascript
复制
      getTimeoffTypes() {

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

}

结果包含我数据,但此数据未绑定到表单选择选项

EN

回答 2

Stack Overflow用户

发布于 2020-09-10 18:06:47

尝试如下所示:

app.service.ts

代码语言:javascript
复制
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();
    }
}

app.component.ts

代码语言:javascript
复制
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();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-10-10 00:29:37

我遇到了同样的问题,选项没有显示出来。下面突出显示的代码帮助解决了这个问题;templateOptions:{ label:“性别”,labelProp:“姓名”,valueProp:“价值”,选项:{“姓名”:“男性”,“价值”:“男性”},{“姓名”:“女性”,“价值”:“女性”},{“姓名”:“他人”,"value“:”其他“},注意:我使用的是Formly材料

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63826599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档