0

I implemented the following set in my template:

<input type="checkbox" [(ngModel)]="something" />
<span *ngIf="something">I'm shown when the checkbox is checked!</span>

I have not declared a variable called something anywhere in my component. Still the checkbox and span are working as expected. Can someone please explain how this can be? Is some property created under the hood? Where and when is this happening?

I'm using WebStorm and a Chrome browser.

1 Answer 1

3

The property is created on your component class, which is nothing more than an object. If a property does not exist on an object it will return as undefined, which will resolve to false. When you check the checkbox, the ngModel will set componentIntance.something to true. At this point the property will be there, when you uncheck the checkbox after this, the property will still be there, but will now be false.

What you are doing there though, does not work anymore with the latest angular versions and with the strict template check turned on. The compiler will detect that the something property is not declared on the component class and will throw an error during compile phase

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

Comments

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.