gpt4 book ai didi

angular - 如何在自定义组件中转换输入数据?

转载 作者:太空狗 更新时间:2023-10-29 17:55:20 24 4
gpt4 key购买 nike

例如,我有组件:

@Component({
selector: 'my-selector',
inputs: ['data'],
moduleId: module.id,
template: '<p>{{transformedData}}</p>'
})
export class MyComponent {
public transformedData: string;
@Input() public data: string;

// How to call this event on "data" change?
public someEvent() {
this.transformedData = this.data + '!';
}
}

如何在 data 变化时调用 someEvent()

最佳答案

onchanges 将在 data 更改时触发。因此,在 onChanges 中,您可以调用 someEvent 函数。

export class MyComponent {
public transformedData: string;
@Input() public data: string;

___________________________________________________________________________________________
// EDIT: you can also do it. Not necessary but can be done this way too.
// Note: If you go with this approach, you don't need to use onchanges hook and don't require above @Input public data line.
@Input() set data(data:string)
console.log(data);
this.someEvent();
}
____________________________________________________________________________________________


ngOnChanges(...args: any[]) {

console.log(args);
this.someEvent();
}

// How to call this event on "data" change?
public someEvent() {
this.transformedData = this.data + '!';
}
}

关于angular - 如何在自定义组件中转换输入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36248577/

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