0

I have a radio button in my Angular project

<input type="radio" ...  checked />

But if I replace checked property on ng-checked ng-checked="true" or on ng-model="flag" (where flag is a component's field and it equals true) it remains unchecked. What is wrong here?

3
  • Can you add a plnkr re-creating the issue. Also, you're using AngularJS not Angular right? Commented Jul 24, 2018 at 17:26
  • What is plnkr? I use Angular, not AngularJS Commented Jul 25, 2018 at 11:59
  • plnkr.co is where you can post running code online, it's often used to show examples. Also if you're using Angular 2+ and not AngularJS then the syntax ng-checked and ng-model won't work since that is AngularJS Commented Jul 25, 2018 at 12:57

2 Answers 2

2

It is simple and should work. But probably somewhere else in the code or in the controller you are changing the flag. But following works with AngularJS.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<input type="radio" ng-checked="true"> Checked radio <br><br>
<input type="radio" ng-checked="false"> Not Checked radio <br><br>
</body>
</html>

But for Angular 2+ here is how you have to do

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

3 Comments

Thanks. But you use angularJS and my project's angular version is the last one
Check my updated answer. I think you are using Angular 2+ and trying to achieve with Angular 1.x syntax.
If that solves your issue, consider accepting the answer to help others.
1

ng-checked and ng-model is AngularJS syntax.

If you are using Angular, you have to use default HTML syntax:

<input type="radio" checked="true"/>

And binding to a property of the Component:
One-way:

<input type="radio" [checked]="myProperty"/>

or two-way:

<input type="radio" [(ngModel)]="myProperty"/>

The "Tour of Heroes" is a really good Tutorial to get started with Angular.

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.