在Angular中,可以使用@Input()
装饰器和属性绑定来传递JSON选项值。以下是一个示例:
在父组件中,定义一个JSON选项对象:
options = {
name: 'John',
age: 25,
gender: 'Male'
};
在父组件的模板中,将选项对象传递给子组件:
在子组件中,使用@Input()
装饰器来接收父组件传递的选项对象:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `
Name: {{ options.name }}
Age: {{ options.age }}
Gender: {{ options.gender }}
`
})
export class ChildComponent {
@Input() options: any;
}
这样,子组件就可以访问和显示父组件传递的JSON选项值了。