3

Let's say I have the following component:

class TestComponent {
  @Input() title;
}

Is there a difference regarding change detection between using it with brackets and without brackets?

<test [title]="title"></test>
<test [title]="'Component Title'"></test>  
<test title="Component Title"></test> 

To be more accurate, the static version, will be checked with every change detection, too?

1 Answer 1

2

Since you declared @Input Angular will create bindings for all of them. It will add it to updateDirectives function that is called during change detection cycle.

So the following

<test [title]="title"></test>
<test [title]="'Title2'"></test>
<test title="Title3"></test>

will be presented as:

updateDirective(_ck, v) {
  var _co = _v.component;
  var currVal_1 = _co.title;
  _ck(_v,4,0,currVal_1);
  var currVal_2 = 'Title2';
  _ck(_v,7,0,currVal_2);
  var currVal_3 = 'Title3';
  _ck(_v,10,0,currVal_3);
}

Live Example

enter image description here

The main difference here is that angular read @Input binding and also create attribute for title="Title3"case. If you don't declare @Input then only attribute will be created.

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

4 Comments

Thanks for this explanation - have deleted my incorrect answer. I hadn't realised that @Input did implicit property binding like this - do you happen to have a reference to the docs that explains this (And perhaps the logic behind it?)
So even if it is a string, it will check it in every change detection? Title3 === Title3 or Title2 === Title2?
@match There is one logic for all @Input. Angular doesn't distinguish whether it's static property or not. It just use the same code for all inputs.
@undefined Yeah, right. At least it's in current version of angular

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.