Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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 角度2.x观察变量变化_Javascript_Angular - Fatal编程技术网

Javascript 角度2.x观察变量变化

Javascript 角度2.x观察变量变化,javascript,angular,Javascript,Angular,我正在从angular 1.x迁移到2.x,但我的大脑仍然在angular 1.x中思考,所以很抱歉提出了愚蠢的问题 我需要的是在我的一个范围变量组件属性发生变化时采取一些措施。我找到了解决办法,但我认为应该有更好的办法 export class MyApp { router: Router; location: Location; fixed: boolean = true; private set isFixed(value:boolean) {

我正在从angular 1.x迁移到2.x,但我的大脑仍然在angular 1.x中思考,所以很抱歉提出了愚蠢的问题

我需要的是在我的一个范围变量组件属性发生变化时采取一些措施。我找到了解决办法,但我认为应该有更好的办法

export class MyApp {
    router: Router;
    location: Location;
    fixed: boolean = true;

    private set isFixed(value:boolean) {
        this.fixed = value;

        //TODO: look here
        console.log('isFixed changed', value);
    }

    private get isFixed():boolean {
        return this.fixed;
    }

    constructor(router: Router, location: Location) {
        this.router = router;
        this.location = location;
    }
}
查看行
console.log('isFixed changed',value)这正是我需要的,而且它正在工作。但是我通过声明
getter
setter
实现了这一点,但是没有更好的方法来监视变量吗?像angular 1.x中一样,是
$scope.$watch

我认为我的组件代码应该是

export class MyApp {
    router: Router;
    location: Location;
    isFixed: boolean = true;

    //TODO: $watch for isFixed change {
        console.log('isFixed changed', value);
    // }

    constructor(router: Router, location: Location) {
        this.router = router;
        this.location = location;
    }
}

您可能希望实现
OnChanges
接口并实现
ngOnChanges()
方法。 只要一个组件的输入或输出绑定值发生更改,就会调用此方法。 另见

Dart代码示例

  @Input() bool fixed;

  @override
  void ngOnChanges(Map<String, SimpleChange> changes) {
    print(changes);
  }
@Input()布尔固定;
@凌驾
无效ngOnChanges(贴图更改){
打印(更改);
}
请参阅(有代码示例)

对你的问题的简短回答是,这实际上只取决于你想做什么。即使这样,也有多种方法来做你想做的事情,即使它不是真正的目的。所以,我认为最好是花几分钟时间看看他们关于组件交互和表单的文档

我个人的偏好是在属性发生变化时使用事件。
ngOnChanges
事件可用于此,但我更喜欢使用
@Input
@Output
,以及()

希望这对你有所帮助,并给你一个你想要的方向。

你可能会觉得有用(对我有用)

基本上,您可以使用
行为子对象
,它允许您为感兴趣的属性设置初始值,然后在注入该服务的任何位置订阅对该属性的更改

e、 g.所以,如果我理解正确的话,可以这样说:

导出类服务{
private fixed=new BehaviorSubject(true);//true是初始值
fixed$=this.fixed.asObservable();
私有集是固定的(值:布尔值){
this.fixed.next(值);
console.log('isFixed changed',值);
}
private get isFixed():布尔值{
返回此.fixed.getValue()
}
构造器(路由器:路由器,位置:位置){
this.router=路由器;
这个位置=位置;
}
}
然后在对
固定值感兴趣的类(例如组件)中:

导出类ObservingComponent{
isFixed:布尔型;
认购:认购;
构造函数(私有someService:someService){}
恩戈尼尼特(){
this.subscription=this.someService.fixed$
.subscribe(fixed=>this.isFixed=fixed)
}
恩贡德斯特罗(){
this.subscription.unsubscripte();
}
}
更新值:

导出类导航{
构造函数(私有someService:someService){}
selectedNavItem(项目:编号){
this.someService.isFixed(true);
}
}

通过此服务自动获取更新值

注:我在Angular 9中进行了测试 我的服务文件

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class SharedService {
    private fixed= new BehaviorSubject<boolean>(false);
    fixed$ = this.fixed.asObservable();

    constructor() {}

    updateFixedValue(value: boolean) {
        this.fixed.next(value);
        console.log('fixed changed', value);
    }
}
您可以从如下任何组件更新或设置新值

this.sharedService.updateFixedValue(your_boolean_value);

谢谢,我希望这对您有用。

您的代码中的
是如何修改的?什么是修改值?@GünterZöchbauer常规属性有类似的解决方案吗?@tchelidze将属性设置为getter/setter,并从setter调用代码。@GünterZöchbauer谢谢。但是,似乎并不自然,房地产本身不应该关心其他人对其变化的反应。我从
Ember
开始,在那里创建
observer
s非常简单。
this.sharedService.updateFixedValue(your_boolean_value);