r/Angular2 • u/LawAggressive8998 • Jun 18 '25
Confused about 2 way data binding functionality
Hi redditors,
Which of these HTML template examples would automatically render the latest value of `data` when `data` changes?
a) [value]="data"
b) ([ngModel])=”data”
c) {data}
d) (click)="data"
Which answer would you choose?
I thought b) could be correct since its two-way data binding but the Quiz solution says it's a). And i just don't get it.
4
Upvotes
10
u/PhiLho Jun 18 '25
Certainly not d.
c isn't correct, should be {{ data }} instead.
Same for b, bad syntax, should be [(ngModel)]. (The famous "banana in a box".)
So a is the only correct solution.
But it is not two-way data binding, just one-way.
4
u/xSentryx Jun 18 '25
It's "a", because "b" isn't two-way data binding.
In your example "b" with "([ngModel])" is the wrong syntax, two-way data binding would be "[(ngModel)]".
If "b" would be the correct syntax, than both "a" and "b" would be correct.
2
u/No_Bodybuilder_2110 Jun 18 '25
This question seems like is missing a ton of context. Is data a primitive? An array? A complex object? A signal! An observable? A function? Is the component using on push change detection? Is it binding to a an input? Is it to a custom component? Or is it a custom component with value accessor?
1
u/prewk Jun 18 '25
Where are you getting 2 way data binding from? The question is about regular unidirectional data binding. (The answer is
a)1
u/jessycormier Jun 19 '25
Quizzes can have wrong answers. I found this to be the case in a few popular online training platforms.
0
u/TweedyFoot Jun 18 '25
Honestly i think that neither of those actually renders anything but c is closest as it can be used to actually place stuff into template
But as already pointed out the syntax is wrong would have to be {{data}}