gpt4 book ai didi

Aurelia:如何观察绑定(bind)对象的特定属性(自定义属性)

转载 作者:行者123 更新时间:2023-12-02 20:41:19 26 4
gpt4 key购买 nike

我正在尝试观察绑定(bind)到自定义属性的对象特定属性的更改事件。我为此使用 bindable 标签。

对象

var information =  {
name: 'foo',
description: 'bar',
age: 12
};

元素

<div my="info: information;"></div>

属性

@customAttribute('my')
export class MyCustomAttribute {

@bindable({
changeHandler: 'change'
})
public info: any;

private change(): void {
console.log('info has changed')
}
}

上面的示例仅触发一次更改处理程序。但我需要它在每次 info 对象上的属性之一发生变化时触发。在我的用例中,哪个属性发生变化并不重要,我只需要知道属性何时发生变化。

知道如何做到这一点吗?

仅供引用 => 另一种方法是在 View 模型上创建一个单独的属性(并在其上使用可绑定(bind)标签)而不是使用对象,但我不想这样做,因为这会使属性的管道变得乏味在 HTML 中(因为属性的数量)。

最佳答案

我用这种方法解决了; (感谢 Marc Scheib 的评论)

import { BindingEngine, Disposable } from 'aurelia-framework';

@customAttribute('my')
export class MyCustomAttribute {

@bindable public info: any;

private subscription: Disposable;

constructor(
private binding_engine: BindingEngine
) { }

public attached(): void {
this.subscription = this.binding_engine
.propertyObserver(this.info, 'name')
.subscribe(() => this.change())
}

private change(): void {
console.log('info name property has changed')
}

public detached(): void {
this.subscription.dispose();
}
}

关于Aurelia:如何观察绑定(bind)对象的特定属性(自定义属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46027878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com