简体   繁体   English

Angular 2组件选择器中的条件

[英]Conditional in Angular 2 Component Selector

With native HTML input tags, I can do bindings on the type attribute like, 使用原生HTML input标签,我可以对类型属性进行绑定,如:

<input [type]="_field.type">

The input element would dynamically change according to the value of _field.type input元素将根据_field.type的值动态变化

However, if I have multiple components like this, 但是,如果我有这样的多个组件,

@Component({
  selector: 'field-option[type=options]',
  templateUrl: ''
})

And use it like, 并使用它,像

<field-option [type]="_field.type">

It does not work, it does not bind. 它不起作用,它不绑定。

I can however, get it to work with static value, 但是,我可以使用静态值,

<field-option [type]="options">

I would like to know how to get this to work? 我想知道如何让它工作?

<input [type]="_field.type">

works because it's handled by the browser. 因为它是由浏览器处理的。

<field-option [type]="_field.type">

would need Angular support but that's not (yet?) implemented. 需要Angular支持,但那不是(还是?)实现的。

As a workaround you could do something like 作为一种解决方法,你可以做类似的事情

<field-option *ngIf="_field.type='type1'" [type]="type1">
<field-option *ngIf="_field.type='type2'" [type]="type2">

I know, cumbersome :-/ 我知道,麻烦: - /

See also https://github.com/angular/angular/issues/6970 另见https://github.com/angular/angular/issues/6970

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM