3

I noticed I must use [value]=true as opposed to value = true in order for my radio button to get the initial value from the component class. How is this working - does putting brackets around value somehow tell Angular to use the initial value from [(ngModel)]?

HTML:

<input type="radio" id ="yesChoice" [(ngModel)] ="serverDeluxe" [value]=true  >
<label for="yesChoice">Yes</label>
<br>
<input type="radio" id ="noChoice"  [(ngModel)] ="serverDeluxe" [value]=false >
<label for="noChoice">No</label>

TS:

export class ServerComponent implements OnInit{
  serverId = 10;
  serverStatus = 'offline';
  serverDeluxe = true;

  ngOnInit(): void {

  }

  getServerStatus(): string{
    return this.serverStatus;
  }
}

2 Answers 2

4

As explained in the Angular documentation:

The brackets, [], tell Angular to evaluate the template expression. If you omit the brackets, Angular treats the string as a constant and initializes the target property with that string:

<app-item-detail childItem="parentItem"></app-item-detail>

Omitting the brackets will render the string "parentItem", not the value of parentItem.

In your case, the brackets allow to set the radio input to a boolean value:

[value]="true" <---- Sets the value to the boolean true
value="true"   <---- Sets the value to the string "true"

The initial value of serverDeluxe is bound to the control with [ngModel] (which is a part of [(ngModel)]). Since serverDeluxe is a boolean property, it will never match the string "true" or "false".

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

4 Comments

thanks so I understand the brackets part now, however how does serverDeluxe bind it's value to [value] on load? For example if it is false, the radio button's will start with false. How is it doing that?
The initial value of serverDeluxe is bound to the control with [ngModel] (which is also a part of [(ngModel)]). I added that part to the answer.
So it is matching to the [value] and then setting checked to true if the value matches?
That is my understanding, and the behavior that we observe. You could can take a look at the code of ngModel, RadioControlValueAccessor and their base classes if you want to know the exact inner workings of the ngModel binding for radio buttons.
0

The above code is working for you because of [value] property. It treats the value as a dynamic value(as template expression) hence html is able to bind the radio option.

If we will provide value=true instead of [value]=true then we will have to provide name property.

For value=true it will take as value="true" and for [value]=true as value=true

2 Comments

thanks but the radio buttons above work fine without the name option, my question is how is my example working without the name option?
Edited my response to the question

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.