如何使用Angular组件观察组件绑定更改 [英] How to watch component binding change using Angular component

查看:120
本文介绍了如何使用Angular组件观察组件绑定更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何收听角度组件绑定更改并执行操作?

How can I listen to angular component binding change and perform actions?

angular.module('myapp')
    .component('myComponent', {
        templateUrl: 'some.html',
        controller: MyController,
        controllerAs: 'myCtrl',
        bindings: {
            items: '<'
        }
    });

现在项目我想要执行的更改使用此值的另一个动作,

我该怎么做?

now when items changes I want to perform another action using this value,
How can I do it?

推荐答案


现在当项目发生变化时我想用这个值执行另一个动作,
我该怎么做?

now when items changes I want to perform another action using this value, How can I do it?

但我想避免使用垂死的$ scope

But I want to avoid using the dying $scope

如果你想要使用 $ scope 您可以使用属性 setter 来检测任何更改,例如:

If you don't want to use $scope you can use a property setter to detect any changes e.g. :

class MyController {
    private _items: string[] = []
    set items(value:string[]){
        this._items = value;
        console.log('Items changed:',value);
    }
    get items():string[]{
        return this._items;
    }
}

const ctrl = new MyController();
ctrl.items = ['hello','world']; // will also log to the console

这篇关于如何使用Angular组件观察组件绑定更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆