0

I normally do property binding like this:

<a [href]="myHref">Link</a>

(where myHref is a property in my Component class.)

But I've also seen

<a href="{{myHref}}">Link</a>

on the Angular website. Which I understand.

But I've also seen this:

<a href={{myHref}}>Link</a>

(without the quotations).

It works, but I can't see this exact syntax being used on the Angular website anywhere. Is it allowed?

1 Answer 1

2

HTML attributes can be done without quotes. I don't think it has anything to do with angular.

Spec says

Attributes are placed inside the start tag, and consist of a name and a value, separated by an = character. The attribute value can remain unquoted if it doesn’t contain spaces or any of " ' ` = < or >. Otherwise, it has to be quoted using either single or double quotes. The value, along with the = character, can be omitted altogether if the value is the empty string.

<a href=http://google.com >Google</a>

From Angular docs

Interpolation is a convenient alternative to property binding in many cases.

When rendering data values as strings, there is no technical reason to prefer one form to the other. You lean toward readability, which tends to favor interpolation. You suggest establishing coding style rules and choosing the form that both conforms to the rules and feels most natural for the task at hand.

When setting an element property to a non-string data value, you must use property binding.

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

3 Comments

Thanks. The thing is that the myHref variable might contain spaces, which would make it invalid HTML, I would have thought, but it seems to work.
I think it's best if everyone would just always use quotes. More consistent and clean :) Though it doesn't matter if the interpolated value has spaces as angular compiler just sees an attribute there and always renders it with quotes anyway.
@funkizer Yes I would agree with you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.