Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS conditionals in HTML Attribute

Tags:

html

angularjs

How would you change an html attribute based on the model?

I'm trying to change an input's placeholder text based on a length of an array:

<input placeholder="{{todos.length ? 'Insert todo' : 'Insert your first todo'}}" />

But that doesn't seem to work...

JS Bin code example.

like image 428
Quang Van Avatar asked Dec 08 '12 18:12

Quang Van


1 Answers

The ternary operator doesn't seem to work in this case, instead of doing this

{{cond ? true : false}}

Change it to

{{ exp && true || false }}

So your placeholder attribute would look like this (I have shortened it for demonstration purposes)

placeholder="{{todos.length > 0 && 'Insert' || 'Insert first'}}"
like image 116
jaime Avatar answered Oct 14 '22 02:10

jaime