【问题标题】:Angular firestore updating document makes element scroll to its topAngular firestore 更新文档使元素滚动到顶部
【发布时间】:2020-03-05 18:42:15
【问题描述】:

点击复选框使父元素滚动到它的顶部。我发现 afs 更新功能正在发生这种情况。如何防止滚动我假设的元素,由于 afs 更新而重新加载?有什么办法可以记住滚动位置并在更新后将其设置回来?

component.html

<div class="taskScrollableContent">
   <div class="taskDescription">
      <p>{{ task.description }}</p>
   </div>
   <div class="todoList">
      <ul *ngFor="let todo of task.todoList; let j = index">
         <li>
           <input type="checkbox" [checked]="todo.isDone" (change)="onTodoCheck(task.todoList, task.id, j)">
           <p [ngClass]="{'done': todo.isDone }">{{ todo.name }}</p>
        </li>
     </ul>
  </div>
</div>

风格

.taskScrollableContent {
    max-height: 250px;
    overflow: auto;
}
.taskScrollableContent::-webkit-scrollbar{
    width: 3px;
}
.taskDescription{
    padding: 0 3px;
}
.taskDescription p{
    word-break: break-word;
    padding: 0;
    margin: 0;
    font-size: 0.9rem;
    line-height: 1rem;
    color: #444;
}
.todoList{
    padding: 0 10px;
    margin-bottom: 10px;
    font-size: 0.8rem;
}
.todoList ul{
    padding: 0;
    margin: 0;
    list-style: none;
}
.todoList li{
    word-break: break-word;
}
.todoList input[type=checkbox]{
    margin-right: 3px;
    vertical-align: text-bottom;
    cursor: pointer;
}
.todoList p{
    padding: 0;
    display: inline;
}
.todoList .done{
    text-decoration: line-through;
}

组件.ts

onTodoCheck(todos: Array<Todo>, taskId: string, i: number): void {
  todos[i].isDone = !todos[i].isDone;
  this.afs.collection('boards').doc(this.boardId)
  .collection('categoryList').doc(this.categoryId)
  .collection('taskList').doc(taskId).update({todoList: todos});
}

【问题讨论】:

    标签: angular firebase google-cloud-firestore angular-directive


    【解决方案1】:

    很抱歉,您 2 年后才收到回复!

    我不确定 Angular,但对于 React,您可以使用数组“欺骗”组件以防止它重新呈现。

    例如,

    // re-renders
    const [data1, setData1] = useState(true);
    
    // doesn't re-render IF you mutate the array directly
    const [data2, setData2] = useState([true]);
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2018-04-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2018-02-24
    • 2020-03-09
    • 2013-09-28
    相关资源
    最近更新 更多