0

Consider the following piece of Angular2 code:

<li *ngFor='let department of departments' value='{{department.id}}'>{{department.name}} {{department.id}}</li>

When I run it in the browser, it renders the following html (cleaned up for clarity):

<li value="-1677026285">Dept1 2617941011</li>

Notice that the same variable, department.id is rendered as its actual value of 2617941011 in the text portion of the li, but as something entirely different in the value attribute (looks like it attempted to parse that string as an integer).

Is there any way to avoid it?

I will add that the Department class (of which departments is an array of) specifies id as a strongly typed property:

    export class Department {constructor(public name:string = "", public id:string = ""){ }}

Thanks in advance!

1
  • Can you provide plukr for the same? Commented Aug 5, 2016 at 18:57

1 Answer 1

2
<li value='{{department.id}}'></li>

This code means that you pass value property on li element.

From this w3schools.com

The value property sets or returns the value of the value attribute of a list item.

Then look at this specification: https://www.w3.org/TR/html5/single-page.html#the-li-element

The value attribute, if present, must be a valid integer giving the ordinal value of the list item.

It should be number from -2147483648 to 2147483647

If the value attribute is present, user agents must parse it as an integer

So there is nothing related with angular2

Use attribute binding instead:

[attr.value]="department.id"
Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense, but any idea how to work around it?
Try [attr.value]='department.id'
Yes, that works. Thank you! I then had to access it from the component as $event.target.attributes['value'].value as opposed to just $event.target.value which is what I was doing pre-workaround.

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.