我创建了一个组件,该组件设置用户对可用帖子的访问权限。 它在职位表中加载帖子,选定的用户和切换。当我选择要检查的切换时,它会向我的Firestore用户doc posts数组添加一个字符串(postId)。当我取消选中它时,它将从数组中删除postId。
但是,当我关闭组件并再次打开它时,每个帖子的切换状态都会丢失。
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef></mat-header-cell>
<mat-cell *matCellDef="let row">
<mat-slide-toggle (change)="changed(row, $event)" >Access</mat-slide-toggle>
</mat-cell>
</ng-container>
changed(post, e: MatSlideToggleChange) {
if (e.checked === true) {
this.auth.grantPostAccess(this.selectedUser, post.id);
} else {
this.auth.removePostAccess(this.selectedUser, post.id);
}
}
如果postId存在于我的字符串数组中,我希望能够将切换状态设置为true。这应该在组件加载之前发生。
答案 0 :(得分:0)
元素中包含某些预定义的属性,您可以根据需要检查或取消选中这些元素:
[checked]="postExists(selectedUser, row.id)"