Angular 2:如何在选项选择中处理* ngif [英] Angular 2: How to handle *ngif in option select

查看:194
本文介绍了Angular 2:如何在选项选择中处理* ngif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用* ngif设置特定的默认选定选项?

How to can i use *ngif for set a specific default selected option?

我需要从数据库中重新加载数据以进行编辑,当我检索数据时,需要在存储值的基础上设置option的默认值.

I need reload data from database for editing, when i retrieve data i need set the default value of option in base a stored value.

我有这个:

<select class="form-control" id="deviceModel">
        <option value="">Select a category</option>
        <option *ngFor='let element of category'*ngIf="{{element}}==={{nameDevice}}" value="{{element}}">{{element}}</option>    
    </select>

谢谢.

推荐答案

ngIf用于结构化操作,基本上是指DOM中是否存在某些内容或已删除了某些内容.如果要绑定到所选属性,可以使用:

ngIf is for structural manipulation, basically whether something is in the DOM, or removed. If you want to bind to the selected property, you could use:

<select class="form-control" id="deviceModel">
    <option value="">Select a category</option>
    <option *ngFor='let element of category' [selected]="element === nameDevice" [value]="element">{{element}}</option>    
</select>

来自 https://angular.io/docs/ts/latest/api/forms/index/SelectControlValueAccessor-directive.html :

如果您的选项值为简单字符串,则可以绑定到普通 选项的value属性.如果您的期权价值恰好是 对象(并且您希望将选择内容保存为表单中的 对象),请改用ngValue.

If your option values are simple strings, you can bind to the normal value property on the option. If your option values happen to be objects (and you'd like to save the selection in your form as an object), use ngValue instead.

如果要使用ngModel绑定,请查看将选择元素绑定到Angular 2中的对象

If you want to use ngModel binding, have a look at Binding select element to object in Angular 2

请注意,在使用属性绑定时,您应该/不应该使用字符串插值

Note that when using property binding, you don't/shouldn't use string interpolation

这篇关于Angular 2:如何在选项选择中处理* ngif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆