Angular 2 - [attr.selected]有效但无选择

时间:2016-09-28 07:41:22

标签: html html5 angular

我有一个简单的问题,但我找不到解决方案

我有这个选择:

<select name="projectObjectUpdate" [ngFormControl]="_projectAmountForm.controls['projectObject']"
                                            class="form-control form-control-select2-field">
                                        <option *ngFor="let object of _projectObjectList" [ngValue]="object" [attr.selected]="object.id === _projectAmountForm.controls['projectObject'].value.id ? true : null">
                                            {{object.descriptions[_user.language]}}
                                        </option>
                                    </select>

In the HTML code, good option is selected

but nothing is selected on screen

谁知道这个问题?

2 个答案:

答案 0 :(得分:1)

您也可以在select元素上绑定ngModel

<select name="projectObjectUpdate" [ngModel]="_projectAmountForm.controls['projectObject'].value.id" [ngFormControl]="_projectAmountForm.controls['projectObject']"
        class="form-control form-control-select2-field" >
    <option *ngFor="let object of _projectObjectList" [ngValue]="object" >
        {{object.descriptions[_user.language]}}
    </option>
</select>

这将始终选择与option

具有相同值的_projectAmountForm.controls['projectObject'].value.id元素

修改

抱歉,是的ngValue错了!我的意思是ngModel。校正。

答案 1 :(得分:-1)

尝试绑定到[selected]属性,如:

<option *ngFor="let object of _projectObjectList" [ngValue]="object" [selected]="object.id === _projectAmountForm.controls['projectObject'].value.id">