加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

角度对象变化检测

发布时间:2020-12-17 17:29:07 所属栏目:安全 来源:网络整理
导读:我有一个第三方角度组件,它是一个显示用户数据的表.如果传递给表的对象发生更改,表将自行更新. Angular如何检测对象的变化?我有以下示例: user.component.ts: @Component({ selector: 'app-user',templateUrl: './user.component.html',styleUrls: ['./us
我有一个第三方角度组件,它是一个显示用户数据的表.如果传递给表的对象发生更改,表将自行更新.

Angular如何检测对象的变化?我有以下示例:

user.component.ts:

@Component({
  selector: 'app-user',templateUrl: './user.component.html',styleUrls: ['./user.component.css'],})
export class UserComponent implements OnInit {

  private _users: User[];
  set users(users: User[]) {
    this._users = users;
  }

  get users(): User[] {
    return this._users;
  }

  constructor() {}

  addUserToTheList(user: User) {

    // this won't be detected as a change to the object
    this.users.push(user);

    // on the other hand,this will
    let userList: User[] = this.users;
    userList.push(user);
    this.users = userList;

  }
}

这是否意味着我必须完全替换对象以触发更改检测,或者我在某种程度上完全忽略了这一点?或者它可能是第三方库(Clarity Design System)的问题

解决方法

您正在使用的表组件可能正在实现ChangeDetectionStrategy.OnPush.

这意味着组件将所有输入视为不可变(意味着它永远不会更改),因此只有在更换输入对象时才会运行更改检测.

这是一个解释它的链接:https://angular-2-training-book.rangle.io/handout/change-detection/change_detection_strategy_onpush.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读