【问题标题】:Angular 2 Reactive Forms, select control filled from serviceAngular 2 Reactive Forms,选择从服务填充的控件
【发布时间】:2017-03-29 22:44:25
【问题描述】:

我正在尝试使用 Angular 的反应式表单,但我无法弄清楚如何延迟由服务填充的下拉列表的默认值绑定。这是我的组件代码的片段:

export class TransferComponent {
id: number;
accounts: Account[] = [];
myForm: FormGroup;
transfer: Transfer;

constructor(
    private http: Http,
    private fb: FormBuilder,
    private route: ActivatedRoute,
    private transferService: TransferService,
    private accountService: AccountService,
    private router: Router) {
    this.transfer = new Transfer();
    this.myForm = fb.group({
        'id': [null],
        'accountFromId': [this.transfer.accountFromId, Validators.required],
        'accountToId': [this.transfer.accountToId, Validators.required],
        'title': [this.transfer.title, Validators.required],
        'amount': [this.transfer.amount, Validators.required],
        'transferDate': [this.transfer.transferDate, Validators.required]
    });
}

ngOnInit(): void {
    this.accountService.getAccountList()
        .then(accounts => this.accounts = accounts);
    this.route.queryParams.subscribe(params => {
        this.id = params['id'];
        if (this.id) {
            this.transferService.getTransfer(this.id)
                .then(transfer => {
                    this.transfer = transfer;
                    this.myForm.setValue(transfer);
                });
        }
    });
}

这里的想法是尝试获取“id”参数,为转移实体调用服务并将其绑定到带有帐户条目的预填充下拉列表的表单。我的部分观点如下所示:

<select class="form-control"
                id="accountFromInput"
                [formControl]="myForm.controls['accountFromId']">
            <option *ngFor="let acc of this.accounts" value="{{acc.id}}">{{acc.name}}</option>
        </select>

大多数字段都正确绑定了传输实体,但“accountFromId”选择元素留下了空值选择(有选项,但未正确选择)。我应该如何重新连接我的组件以确保在从服务中获取帐户值并将它们添加到选择后绑定 accountFromId?

【问题讨论】:

    标签: angular angular2-forms angular2-services reactive-forms


    【解决方案1】:

    原来我在寻找 Promise.all()。我必须等待服务中的 transfer 和 account[] 实体,然后绑定到表单。更新和工作代码如下:

    export class TransferComponent {
    id: number;
    accounts: Account[] = [];
    myForm: FormGroup;
    submitted: boolean = false;
    saved: boolean = false;
    
    constructor(
        private http: Http,
        private fb: FormBuilder,
        private route: ActivatedRoute,
        private transferService: TransferService,
        private accountService: AccountService,
        private router: Router) {
        this.myForm = fb.group({
            'id': [null],
            'accountFromId': [null, Validators.required],
            'accountToId': [null, Validators.required],
            'title': [null, Validators.required],
            'amount': [null, Validators.required],
            'transferDate': [null, Validators.required]
        });
    }
    
    ngOnInit(): void {
        var accountPromise:Promise<Account[]> = this.accountService.getAccountList()
            .then(accounts => this.accounts = accounts);
        this.route.queryParams.subscribe(params => {
            this.id = params['id'];
            if (this.id) {
                var transferPromise: Promise<Transfer> = this.transferService.getTransfer(this.id);
                Promise.all([
                    accountPromise,
                    transferPromise
                ]).then(value => {
                    this.myForm.setValue(value[1]);
                })
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2018-08-24
      • 1970-01-01
      • 2021-08-30
      • 2017-05-06
      • 1970-01-01
      相关资源
      最近更新 更多