简体   繁体   English

设置默认选择列表值Angular2

[英]Set default select list value Angular2

I want to set the default option for the select in angular 2 as "Select an option". 我想将角度2中的选择设置为“选择选项”。 Here is the code that I have so far: 这是我到目前为止的代码:

HTML 的HTML

<div class="form-group">
                        <label class="col-md-4 control-label" for="CustomFix">Select an option:</label>
                        <div class="col-md-4">
                            <select id="example" name="example" class="form-control" [(ngModel)]="exampleArray" (ngModelChange)="changes()">
                                <option disabled selected>Select a Custom Fix</option>
                                <option *ngFor="let example of exampleArray" [ngValue]="example">{{example.attribute }}</option>
                            </select>
                        </div>
                    </div>

Typescript 打字稿

changes(){
console.log(this.example.option);
}

I have the line in my html: 我的html中有这一行:

<option disabled selected>Select a Custom Fix</option>

How can I enable this as the default option? 如何启用它作为默认选项? It is separate from my array used with ngModel 它与我与ngModel一起使用的数组分开

If you want to have this option selected at the start - that's usually when the ngModel 's value is undefined , you just have to tell Angular that this option is responsible for undefined value, so it should be: 如果您想在一开始就选择此选项-通常是在ngModel的值undefined ,您只需要告诉Angular该选项负责undefined值,因此应该是:

<option disabled [ngValue]="undefined">Select a Custom Fix</option>

You also need do correct your [(ngModel)] binding - right now you're trying to bind selected value to.. the array itself. 您还需要更正[(ngModel)]绑定-现在您正试图将所选值绑定到数组本身。 You should rather bind to some other property, eg: 您应该绑定到其他属性,例如:

<select id="example" name="example" class="form-control" [(ngModel)]="example">

(You can see the working solution here: http://plnkr.co/edit/Zu29ztqaaDym1GYDAhtJ?p=preview ) (您可以在此处查看有效的解决方案: http : //plnkr.co/edit/Zu29ztqaaDym1GYDAhtJ?p=preview

You should give that option a value, bind the select element to an ID variable, and set that variable on component load. 您应该给该选项一个值,将select元素绑定到ID变量,并在组件加载时设置该变量。

 // controller exampleArraySelectedValue = -1; 
 <div class="form-group"> <label class="col-md-4 control-label" for="CustomFix">Select an option:</label> <div class="col-md-4"> <select id="example" name="example" class="form-control" [(ngModel)]="exampleArraySelectedValue" (ngModelChange)="changes()"> <option value="-1">Select a Custom Fix</option> <option *ngFor="let example of exampleArray" [ngValue]="example">{{example.attribute }}</option> </select> </div> </div> 

If you use [ngValue] then you need to assign the identical object instance to exampleArray . 如果您使用[ngValue]那么你需要一个对象实例分配给exampleArray Another object instance with the same properties and values won't do. 具有相同属性和值的另一个对象实例将不起作用。

If you use [value]="..." instead of [ngValue] , then only strings can be used and for string comparison to different string instances containing the same characters are considered equal, but that's not the case for object instances where exampleArray needs to reference the exact same object reference used with [ngValue] . 如果使用[value]="..."而不是[ngValue] ,则只能使用字符串,并且与包含相同字符的不同字符串实例进行字符串比较时,它们被认为是相等的,但对于exampleArray对象实例则不是这种情况需要引用与[ngValue]一起使用的完全相同的对象引用。

Actually 其实

[(ngModel)]="exampleArray"

in your example is invalid, because the model should not be the array that is used to generate the <option> elements, it should be a property that holds the selected value. 在您的示例中是无效的,因为模型不应该是用于生成<option>元素的数组,它应该是保存所选值的属性。

   <div class="form-group">
        <label class="col-md-4 control-label" for="CustomFix">Select an option:</label>
        <div class="col-md-4">
            <select id="example" name="example" class="form-control" [(ngModel)]="selectedItem" (ngModelChange)="changes()">
                <option disabled selected>Select a Custom Fix</option>
                <option *ngFor="let example of exampleArray" [ngValue]="example">{{example.attribute }}</option>
            </select>
        </div>
    </div>
constructor() {
  this.selectedItem = exampleArray[1]; // will make the 2nd element of `exampleArray` the selected item
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular2中的默认选择选项不起作用 - Default select option in angular2 not working 如何在Angular2中动态设置* ngFor的值 - How to set value of *ngFor dynamically in Angular2 ng-Select Option如果使用angularJS匹配值,则从列表中设置默认值 - ng-Select Option set default value from list if value match using angularJS Angular2:绑定时出现运行时错误 <select>组合框到从ngOnInit()中的API检索到的对象的列表 - Angular2: runtime error when binding a <select> combo box to a list of objects retrieved from an API in ngOnInit() 如何使用angular js将选择下拉列表中的值填充为默认值 - How to populate value in a select dropdown as default using angular js 如何设置由SQL查询提交的HTML select元素的默认值? - How to set default value for HTML select element that is submited by sql query? Angular2:无法在输入类型为“ date”的情况下从ngModel设置初始值 - Angular2: Unable to set initial value from ngModel on input type=“date” 如何为HTML选择元素设置默认消息值? - How to set default message value for HTML select element? 在下拉列表中预选择默认值 - Pre-select default value in drop down list Angular2在组件中设置音频currentTime - Angular2 set audio currentTime in component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM