从选择的ng-model ANGULAR 8中删除空白选项

时间:2020-10-05 12:51:29

标签: html angular typescript frontend angular-ngmodel

我将从选择的ng-model中删除空白的first选项,而是放置数组的第一个值。 这是我的代码:

 <div class="col-9">
    <select ng-model="chooseOrg"
            formControlName="orgTypeId"
            class="editmodal-object">
            <option [ngValue]="orgType.id" *ngFor="let orgType of allOrgType" style="text-transform:uppercase;">{{orgType.description}}</option>
    </select>
 </div>

我的数组内部没有空白值,但是它由5个对象组成。我只希望第一个始终是我选择的默认值。

1 个答案:

答案 0 :(得分:1)

由于您使用的是ngmodel,因此在更改formControlName时应该对其进行更改。我认为同时使用这两种方法都会使其无法正常工作。因此,删除ngmodel并依靠formControlName并检查更改并以这种方式设置selectOrg。

要知道的另一件好事是,您可以通过在诸如此类的方法中将默认值设置为1来设置新FormControl()的默认值:

new FormControl(1)

我为您进行了一次突击,向您展示了我的意思。 https://stackblitz.com/edit/angular-ivy-klwglz?file=src/app/app.component.html