How to mark check box as a checked in angular 4
I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like
<tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>
i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript
document.getElementById("role").checked
but in angular 4 there is no property like that.
i searched and found there is one property binding for
[checked] ="somevariable"
but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.
other solution I have in jquery like
$(document.getElementById(role)).prop('checked', true);
but might be it can create problem, i am not sure please correct me.
please help me. any clue or logic will same my days.
angular angular4-forms
add a comment |
I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like
<tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>
i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript
document.getElementById("role").checked
but in angular 4 there is no property like that.
i searched and found there is one property binding for
[checked] ="somevariable"
but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.
other solution I have in jquery like
$(document.getElementById(role)).prop('checked', true);
but might be it can create problem, i am not sure please correct me.
please help me. any clue or logic will same my days.
angular angular4-forms
1
MakesameVariable
a property ofroleObj
, and only update the relevant ones
– bugs
Apr 18 at 16:23
add a comment |
I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like
<tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>
i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript
document.getElementById("role").checked
but in angular 4 there is no property like that.
i searched and found there is one property binding for
[checked] ="somevariable"
but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.
other solution I have in jquery like
$(document.getElementById(role)).prop('checked', true);
but might be it can create problem, i am not sure please correct me.
please help me. any clue or logic will same my days.
angular angular4-forms
I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like
<tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>
i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript
document.getElementById("role").checked
but in angular 4 there is no property like that.
i searched and found there is one property binding for
[checked] ="somevariable"
but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.
other solution I have in jquery like
$(document.getElementById(role)).prop('checked', true);
but might be it can create problem, i am not sure please correct me.
please help me. any clue or logic will same my days.
angular angular4-forms
angular angular4-forms
asked Apr 18 at 16:20
user3364549
12217
12217
1
MakesameVariable
a property ofroleObj
, and only update the relevant ones
– bugs
Apr 18 at 16:23
add a comment |
1
MakesameVariable
a property ofroleObj
, and only update the relevant ones
– bugs
Apr 18 at 16:23
1
1
Make
sameVariable
a property of roleObj
, and only update the relevant ones– bugs
Apr 18 at 16:23
Make
sameVariable
a property of roleObj
, and only update the relevant ones– bugs
Apr 18 at 16:23
add a comment |
3 Answers
3
active
oldest
votes
You can modify your object to also include a boolean checked
property (i.e. roleObj.checked = false
) and dynamically update the relevant ones when you need to.
Then you markup simply becomes
<input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
add a comment |
You can follow this simple implementation
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-container *ngFor="let item of items">
<input type="checkbox"
[checked]="selected === item.id "
[value]="item.id"
(change)="selected = item.id"
[attr.id]="item.id"
/>
<label [attr.for]="item.id"> {{ item.label }}</label>
</ng-container>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
items = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' }
];
selected = 1;
}
Live demo
add a comment |
You should use variable. In your .ts:
public checkboxValue: boolean;
constructor() {
this.checkboxValue = false;
}
And in your .html:
<input type="checkbox" [(ngModel)]="checkboxValue"/>
You should remember to add FormModule in your @NgModule:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49904531%2fhow-to-mark-check-box-as-a-checked-in-angular-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can modify your object to also include a boolean checked
property (i.e. roleObj.checked = false
) and dynamically update the relevant ones when you need to.
Then you markup simply becomes
<input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
add a comment |
You can modify your object to also include a boolean checked
property (i.e. roleObj.checked = false
) and dynamically update the relevant ones when you need to.
Then you markup simply becomes
<input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
add a comment |
You can modify your object to also include a boolean checked
property (i.e. roleObj.checked = false
) and dynamically update the relevant ones when you need to.
Then you markup simply becomes
<input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />
You can modify your object to also include a boolean checked
property (i.e. roleObj.checked = false
) and dynamically update the relevant ones when you need to.
Then you markup simply becomes
<input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />
edited Nov 10 at 23:51
answered Apr 18 at 16:26
bugs
4,9665723
4,9665723
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
add a comment |
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
– user3364549
Apr 19 at 5:37
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
– bugs
Apr 19 at 7:13
add a comment |
You can follow this simple implementation
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-container *ngFor="let item of items">
<input type="checkbox"
[checked]="selected === item.id "
[value]="item.id"
(change)="selected = item.id"
[attr.id]="item.id"
/>
<label [attr.for]="item.id"> {{ item.label }}</label>
</ng-container>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
items = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' }
];
selected = 1;
}
Live demo
add a comment |
You can follow this simple implementation
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-container *ngFor="let item of items">
<input type="checkbox"
[checked]="selected === item.id "
[value]="item.id"
(change)="selected = item.id"
[attr.id]="item.id"
/>
<label [attr.for]="item.id"> {{ item.label }}</label>
</ng-container>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
items = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' }
];
selected = 1;
}
Live demo
add a comment |
You can follow this simple implementation
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-container *ngFor="let item of items">
<input type="checkbox"
[checked]="selected === item.id "
[value]="item.id"
(change)="selected = item.id"
[attr.id]="item.id"
/>
<label [attr.for]="item.id"> {{ item.label }}</label>
</ng-container>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
items = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' }
];
selected = 1;
}
Live demo
You can follow this simple implementation
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-container *ngFor="let item of items">
<input type="checkbox"
[checked]="selected === item.id "
[value]="item.id"
(change)="selected = item.id"
[attr.id]="item.id"
/>
<label [attr.for]="item.id"> {{ item.label }}</label>
</ng-container>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
items = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' }
];
selected = 1;
}
Live demo
answered Apr 18 at 16:28
Tomasz Kula
6,5591331
6,5591331
add a comment |
add a comment |
You should use variable. In your .ts:
public checkboxValue: boolean;
constructor() {
this.checkboxValue = false;
}
And in your .html:
<input type="checkbox" [(ngModel)]="checkboxValue"/>
You should remember to add FormModule in your @NgModule:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
add a comment |
You should use variable. In your .ts:
public checkboxValue: boolean;
constructor() {
this.checkboxValue = false;
}
And in your .html:
<input type="checkbox" [(ngModel)]="checkboxValue"/>
You should remember to add FormModule in your @NgModule:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
add a comment |
You should use variable. In your .ts:
public checkboxValue: boolean;
constructor() {
this.checkboxValue = false;
}
And in your .html:
<input type="checkbox" [(ngModel)]="checkboxValue"/>
You should remember to add FormModule in your @NgModule:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
You should use variable. In your .ts:
public checkboxValue: boolean;
constructor() {
this.checkboxValue = false;
}
And in your .html:
<input type="checkbox" [(ngModel)]="checkboxValue"/>
You should remember to add FormModule in your @NgModule:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
edited Apr 18 at 16:54
Zach Newburgh
35729
35729
answered Apr 18 at 16:23
kris_IV
484521
484521
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49904531%2fhow-to-mark-check-box-as-a-checked-in-angular-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Make
sameVariable
a property ofroleObj
, and only update the relevant ones– bugs
Apr 18 at 16:23