Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度4-观察变量的变化特性_Javascript_Angular - Fatal编程技术网

Javascript 角度4-观察变量的变化特性

Javascript 角度4-观察变量的变化特性,javascript,angular,Javascript,Angular,我想查看json的嵌套属性。每当此嵌套属性发生更改时,调用fn() Fn更改值 public changeUser(item) { this.user.selected = item; /*Some Code here*/ } public customLogin(user) { /*Some Code here*/ this.user.selected = user; /*Some Code here*/ } 每当此.us

我想查看json的嵌套属性。每当此嵌套属性发生更改时,调用fn()

Fn更改值

public changeUser(item) {
    this.user.selected = item;
    /*Some Code here*/
}

  public customLogin(user) {
        /*Some Code here*/
        this.user.selected = user;
        /*Some Code here*/
}
每当
此.user.selected的值更改时,调用函数。
我也在使用
rxjx


有什么建议吗

您可以这样做:

export class HeaderComponent implements OnDestroy {
  user: any;
  userSelectSubject: BehaviorSubject<{name: string, img: string}>;
  private userSelectSubscription: Subscription;

  constructor(){
    this.user = {
      options: [
        { name: 'Jenny Hess', img: 'assets/img/avatar/small/jenny.jpg' },
        { name: 'Elliot Fu', img: 'assets/img/avatar/small/elliot.jpg' },
        { name: 'Stevie Feliciano', img: 'assets/img/avatar/small/stevie.jpg' }
      ]
    }

    this.userSelectSubject = new BehaviorSubject<{name: string, img: string}>({ name: 'Jenny Hess', img: 'assets/img/avatar/small/jenny.jpg' });

    this.userSelectSubscription = this.userSelectSubject.subscribe((newSelectedUser) => {
      this.user.selected = newSelectedUser;
    });
  }

  ngOnDestroy() {
    this.userSelectSubscription.unsubscribe();
  }
}
导出类标题组件实现OnDestroy{
用户:任何;
用户选择主题:行为主题;
private userSelectSubscription:Subscription;
构造函数(){
此用户={
选项:[
{name:'Jenny Hess',img:'assets/img/avatar/small/Jenny.jpg'},
{name:'Elliot Fu',img:'assets/img/avatar/small/Elliot.jpg'},
{name:'Stevie Feliciano',img:'assets/img/avatar/small/Stevie.jpg'}
]
}
this.userSelectSubject=newbehaviorsubject({name:'Jenny Hess',img:'assets/img/avatar/small/Jenny.jpg'});
this.userSelectSubscription=this.userSelectSubject.subscripte((newSelectedUser)=>{
this.user.selected=newSelectedUser;
});
}
恩贡德斯特罗(){
此.userSelectSubscription.unsubscribe();
}
}

然后只需调用
this.userSelectSubject.next({…})
将新选择的用户作为参数传递。

HeaderComponent
需要查看值或其他组件(父组件)?@肾上腺素DJ编号
此用户的值。所选
通过函数在
HeaderComponent
中更改。提供更多信息,说明将以何种方式更改。您可以尝试使用
ngDoCheck
。提供详细信息,如:如何准确更改属性,您希望在何处观察该更改?@dee zg@YordanNikolov编辑了添加
Fn更改值的帖子
谢谢@giovain It Helped导入订阅,但出现错误。“订阅不是一个函数”,我错过了什么。?
export class HeaderComponent implements OnDestroy {
  user: any;
  userSelectSubject: BehaviorSubject<{name: string, img: string}>;
  private userSelectSubscription: Subscription;

  constructor(){
    this.user = {
      options: [
        { name: 'Jenny Hess', img: 'assets/img/avatar/small/jenny.jpg' },
        { name: 'Elliot Fu', img: 'assets/img/avatar/small/elliot.jpg' },
        { name: 'Stevie Feliciano', img: 'assets/img/avatar/small/stevie.jpg' }
      ]
    }

    this.userSelectSubject = new BehaviorSubject<{name: string, img: string}>({ name: 'Jenny Hess', img: 'assets/img/avatar/small/jenny.jpg' });

    this.userSelectSubscription = this.userSelectSubject.subscribe((newSelectedUser) => {
      this.user.selected = newSelectedUser;
    });
  }

  ngOnDestroy() {
    this.userSelectSubscription.unsubscribe();
  }
}