10

I'm trying to learn the Angular 2. I have a simple material input tag and I wish to set it's value on a conditon.

<md-input value="dataSelected ? {{selectedDataName}} : ''"></md-input>

I tried to set [value] or dataSelected as {{dataSelected}} or (dataSelected) but it doesn't do the trick. Can someone lead me into right direction?

2
  • are you getting any error when you set it ? Commented Dec 16, 2016 at 8:05
  • No errors. If i leave it as: value="dataSelected ? {{selectedDataName}} : '' " The result in field is: dataSelected ? TestName1 : '' - so it correctly converted only the selectedDataName. What should be the correct syntax in your opinion? I'll test it out and show you the result. Commented Dec 16, 2016 at 8:09

1 Answer 1

15

Instead of value="dataSelected ? {{selectedDataName}} : ''" you can set it value with below possibile ways :

<md-input [attr.value]="dataSelected ? selectedDataName : ''"></md-input>

or

<md-input [value]="dataSelected ? selectedDataName : ''"></md-input>

or

<md-input value="{{dataSelected ? selectedDataName : ''}}"></md-input>
Sign up to request clarification or add additional context in comments.

4 Comments

It seems that <md-input [value]="dataSelected ? selectedDataName : ''"></md-input> had worked as expected. Thank you !
@Morgoth Not sure if it's for different browsers or if it's the tooth of time biting the suggesting but on my system, I get a title popup anyway. The resolution I had to use is binding to a backing field being set condition?"title text":null. So vasically the same junk as suggested but my unk has to be null (or undefined) not empty string.
@KonradViltersten This short article explains that null causes Angular to remove/completely omit the attribute itself, instead of adding an empty attribute. Small but important difference for your case (and mine).
@KonradViltersten just to add a nasty caveat to the previous comment: [maxlength]="null" omits the attribute, but [maxLength]="null" (notice the capital L) converts to maxlength="0" (!!!)

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.