Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了typescript – 使用异步数据加载在Angular2中选择选项大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个表单来更新客户对象.

我有一个客户类型的选择,我想在获得客户数据时预先选择当前值.
我怎样才能做到这一点 ?

这是我的html表单的一部分:

<div class="form-group">
    <label for="type" class="req">Type</label>
    <select class="form-control" ngControl="type">
        <option *ngFor="#p of typecustomerlist" [value]="p.code">{{p.label}}</option>
    </select>
    <div [hidden]="type.valid" class="alert alert-danger">
        Please select a type
    </div>
</div>
<div class="form-group">
    <label for="lastname" class="req">Last name</label>
    <input type="text" ngControl="lastname" value="{{customer.Lastname}}" />
    <div *ngIf="lastname.dirty && !lastname.valid" class="alert alert-danger">
        <p *ngIf="lastname.errors.required">Lastname is required.</p>
    </div>
</div>

解决方法

我找到了解决方案.我在我的组件ngOnInit()方法添加了这个:this.type.updateValue(this.customer.type);

我的组件:

export class UpdateCustomerComponent implements OnInit {
    myCustomerForm: ControlGroup;
    lastname: Control;
    type: Control;

    constructor(routeParam: RouteParams,private dataService: ContactDataService,private builder: FormBuilder) {
        this.lastname = new Control('',Validators.compose([Validators.required])
        );

        this.type = new Control(this.customer != null ? this.customer.Type : null,Validators.compose([Validators.required])
        );

        this.myCustomerForm = builder.group({
            lastname: this.lastname,type: this.type
        });
    }

    ngOnInit() {

        this.dataService.get(this.id).subscribe(data => {
            this.customer = <ICustomer>JSON.parse(JSON.stringify(data));

            this.lastname.updateValue(this.customer.Lastname);
            this.type.updateValue(this.customer.Type);
        });

    }

}

大佬总结

以上是大佬教程为你收集整理的typescript – 使用异步数据加载在Angular2中选择选项全部内容,希望文章能够帮你解决typescript – 使用异步数据加载在Angular2中选择选项所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: