ANGULAR2 Select Usage-Set default values and events

Source: Internet
Author: User

Set default values:
Now there are three students xiaoming, Little Red, little black, on two-way bound student set the select value you want can be selected by default in the dropdown box
Code1:
Set "Select" as the default, just set the variable student to ", you can default to" please select "
Scenario 1:

TS:
students:string[]=[' xiaoming ', ' xiaohong ', ' Xiaohei '];
Student:string= ";

HTML:
  <select [(Ngmodel)]= "student" >
        <option value= "" > Please select </option>//Here is value instead of [ Value]
        <option *ngfor= "Let item of Students" [value]= ' item ' >{{item}}</option>  
    </select>

Scenario 2:

TS:
students:string[]=[', ' xiaoming ', ' xiaohong ', ' Xiaohei '];
Student:string= ";

HTML:
<select [(Ngmodel)]= "student" >
  <option *ngfor= "Let item of Students" [value]= ' Item ' >{{ Item | | ' Please select '}}</option>  
</select>

Code2:
When you need to set the default value to Xiaoming, just set the initial value of the variable student to "Xiaoming"

TS:
students:string[]=[' xiaoming ', ' xiaohong ', ' Xiaohei '];
student:string= ' xiaoming ';

HTML:
    <select [(Ngmodel)]= "student" >
        <option *ngfor= "Let item of Students" [value]= ' Item ' >{{item }}</option>  
    </select>

Binding Events
Select drop-down box for selecting events primarily through Ngmodel and Ngmodelchange
If you want to trigger an event when you select an item in the Select drop-down box, you can split [(Ngmodel)] into Ngmodel and ngmodelchange to implement

TS:
students:string[]=[' xiaoming ', ' xiaohong ', ' Xiaohei '];
Student:string= ";
Info:string= ";
SetInfo () {
    this.info=student;
}

HTML:
<select [ngmodel]= "Student" (ngmodelchange) = "student= $event; SetInfo ()" >
    <option value= "" > Please select </option>
    <option *ngfor= "Let item of Students" [value]= ' Item ' >{{item}}</option>  
</select>
{{Info}}

In property bindings, a value is passed from the model to the target property on the screen. We mark the target property by enclosing the name in square brackets, []. This is a one-way data binding from the model to the view.

In event bindings, values are passed from the target property on the screen to the model. We mark the target attribute by enclosing the name in parentheses, (). This is a reverse one-way data binding from view to model.

When the binding target of [(x)] in Angular2, his input and output attributes are represented by X and Xchange.

The student= $event principle in the code is as follows Ngmodelchange is a property of a Angular eventemitter type, and when it fires, it returns the value of the input box.

Click to view more about Ngmodel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.