3

With native HTML input tags, I can do bindings on the type attribute like,

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

The input element would dynamically change according to the value of _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?

6
  • Did you try attribute binding? <field-option [attr.type]="_field.type"> Commented Feb 23, 2016 at 9:15
  • See github.com/angular/angular/issues/6970#issuecomment-182025921 Commented Feb 23, 2016 at 9:17
  • 1
    Ah, guess it's a bug ;( Commented Feb 23, 2016 at 9:19
  • @Sasxa [type]="_field.type" this doesn't do anything to DOM (no type attribute at all), [attr.type]="_field.type" does adds type attributes, but doesn't renders the right component. Confusing. Commented Feb 23, 2016 at 9:48
  • 1
    Does your <field-option> component have @Input() type? Maybe you could just use selector 'field-option' and use type as property... Commented Feb 23, 2016 at 9:52

1 Answer 1

2
<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.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.