Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Angular 2中的选择控件中显示占位符(空选项)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的模板中有这个代码
<SELEct [ngModel]="SELEctedSubSectionId" (ngModelChangE)="onSubSectionChange($event)">
  <option *ngFor="let subSection of event.subSections" [ngValue]="subSection.id">{{ subSection.name }}</option>
</SELEct>

在我的组件中:

public SELEctedSubSectionId: any;

public onSubSectionChange(subSectionId: any) {
  // some code I execute after ngModel changes.
}

这工作正常,但一开始我有一个空盒子.我想在那里显示一个占位符消息.如何使用NgModel执行此操作?

我的解决方

在组件typescript文件中,我添加一个我没有初始化的属性SELEctUndefinedoptionValue,在html中我将undefinedSELEctOptionValue添加为占位符选项的值.此解决方案适用于数字和字符串模型属性.

@Component({
  SELEctor: 'some-component-SELEctor',templateUrl:'url to template',})
export class someComponent implements OnInit {
    private SELEctUndefinedoptionValue:any;
    private someObject:SomeObject;
    
    ngOnInit() {
      someObject = new SomeObject();
    }
}
<SELEct [(ngModel)]="someObject@L_893_13@mePropertyId">
  <option disabled hidden [value]="SELEctUndefinedoptionValue">-- SELEct --</option>
  <option *ngFor="let option of options" [value]="option.id">option.text</option>
</SELEct>

大佬总结

以上是大佬教程为你收集整理的如何在Angular 2中的选择控件中显示占位符(空选项)?全部内容,希望文章能够帮你解决如何在Angular 2中的选择控件中显示占位符(空选项)?所遇到的程序开发问题。

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

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