xx.value as xx.name for xx in yy in the select tag of Angular

Purpose: Loop through the array Arr, get each item in Arr, and display the name of the item as the option value. The array is composed of objects, and each object has two attributes: name and value.

Above code:

<select ng-model="oParam.szReboot" ng-options="item.value as item.name for item in arr"></select>

Example analysis: item.value as item.name for item in arr . for refers to the value in the option tag in html5, and the as tag means the value corresponding to the value in the option tag. That is equivalent to:

<option value="item.id">item.name</option>

When the command is issued, the passed that.oParam.szReboot is the value value , and the page refresh will also display the name value of the corresponding option according to the obtained value value.

If you want to add a "please select" option in front of the option, you only need to add an option tag line, the code is as follows:

<select id="type" ng-model="type" ng-options="item.id as item.name for item in items">

  <option value=“-1”>请选择</option>

</select>

Note: When using the select tag, the ng-options directive should be used directly in the select tag instead of the ng-repeat directive in the option tag to prevent the first line of the drop-down box from appearing blank .

Guess you like

Origin blog.csdn.net/weixin_44427784/article/details/118599454