Arrays 转换JSON对象数组
因此,我有一个复选框,用于捕捉值角色。每个角色如下所示: 当我选中复选框列表中的几个角色时,我将它们放入数组selectedRoles中 我遇到的问题是,当我转到console.log并打印结果console.logthis.selectedRoles 我得到2[[object object]、[object object]]或更多,这取决于我选择了多少角色。我无法将它们推出,以便可以查看每个角色的属性,因为我希望它们与服务一起使用。我用的是角度2 html: ts: 将值替换为ngValue指令:Arrays 转换JSON对象数组,arrays,json,angular,object,Arrays,Json,Angular,Object,因此,我有一个复选框,用于捕捉值角色。每个角色如下所示: 当我选中复选框列表中的几个角色时,我将它们放入数组selectedRoles中 我遇到的问题是,当我转到console.log并打印结果console.logthis.selectedRoles 我得到2[[object object]、[object object]]或更多,这取决于我选择了多少角色。我无法将它们推出,以便可以查看每个角色的属性,因为我希望它们与服务一起使用。我用的是角度2 html: ts: 将值替换为ngValue指
<input class="roles-li" type="checkbox" [checked]="userRolesNames.indexOf(role.name)>-1" name="role" [ngValue]="role" (change)="selectRole(option, $event)">
在您的ts中:
我建议不要为复选框将值绑定到对象。相反,我只是在DOM中传递带有函数的角色
(change)="selectRole(role, option, $event)"
这最终会变得更清晰、更容易理解。我们需要更多地了解您的编解码器。您可以显示记录阵列的代码吗?如果您还需要其他内容,请告诉我。如果要记录对象,请先序列化它。我得到错误:无法绑定到“ngvalue”,因为它不是“input”的已知属性。nput class=roles li type=checkbox[checked]=userRolesNames.indexofore.name>-1 name=role[ERROR->][ngvalue]=role change=selectRoleoption,$event>{{role.label}我编辑了我的答案,尝试用[ngvalue]代替[ngvalue]你能告诉我你使用的是哪个版本的angular吗?@angular/common:^4.0.0,我认为问题出在Angular的版本中,现在尝试使用[value],如果它不起作用,我建议将角色作为参数直接传递给selectRole方法
<input class="roles-li" type="checkbox" [checked]="userRolesNames.indexOf(role.name)>-1" name="role" [ngValue]="role" (change)="selectRole(option, $event)">
<input class="roles-li" type="checkbox" [checked]="userRolesNames.indexOf(role.name)>-1" name="role" (change)="selectRole(option, $event, role)">
selectRole(category, event , role) {
var index = this.selectedRoles.indexOf(role);
if (event.target.checked) {
this.selectedRoles.push(role);
} else {
if (index !== -1) {
this.selectedRoles.splice(index, 1);
}
}
console.log(this.selectedRoles);
}
(change)="selectRole(role, option, $event)"