代码之家  ›  专栏  ›  技术社区  ›  FiringBlanks

你能调整一个角度组件每次使用时使用不同的值吗?

  •  0
  • FiringBlanks  · 技术社区  · 6 年前

    <ice-cream-cone></ice-cream-cone>
    <ice-cream-cone></ice-cream-cone>
    <ice-cream-cone></ice-cream-cone>
    

    问题是上面的组件调用将导致它们看起来完全相同。可以调整指令以触发组件中特定值的加载吗?我希望有这样的事情:

    <ice-cream-cone flavor="vanilla"></ice-cream-cone>
    <ice-cream-cone flavor="chocolate"></ice-cream-cone>
    <ice-cream-cone flavor="rocky_road"></ice-cream-cone>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   abdullahkady    6 年前

    当然可以,这可以使用Input binding属性来完成

    @Component({
      selector: 'ice-cream-cone',
      template: `
        {{flavor}}
      `
    })
    class IceCreamComponent {
      @Input('flavor') flavor: string;
    } 
    

    然后调用组件时,您将执行以下操作:

    <ice-cream-cone flavor="vanilla"></ice-cream-cone>
    

    看看这里: Docs