You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular中使用对象绑定组件的多个属性?

组件对象批量绑定:Vue、React vs Angular

Vue中的实现

<Person v-bind='{name: "Tom", color: "Blue"}'></Person>

React中的实现

<Person {...{name: "Tom", color: "Blue"}}></Person>

Angular中如何实现相同效果?


更新说明

Person组件定义了两个输入属性,需要将一个对象快速绑定到该组件:

数据示例:

const it = {name: "Tom", color: "Blue"}

Person组件代码:

@Component({
  templateUrl: './person.component.html'
})
export class PersonComponent {
    @Input() name: string;
    @Input() color: string;
}

Angular的解决方案

如果你的Angular版本在14及以上,可以直接使用Angular新增的输入属性展开语法,和Vue/React的写法逻辑一致,直接将对象展开绑定到组件:

<Person [...]="it" />

如果是Angular 14以下的版本,只能逐个绑定对象属性到组件输入:

<Person [name]="it.name" [color]="it.color" />

内容的提问来源于stack exchange,提问作者scccboom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:32:03