将对象作为属性Angular传递给指令

时间:2018-11-17 15:31:24

标签: angular typescript angular7

我正在尝试将数据传递给组件,看来我只能传递字符串,有没有一种方法可以使角度将数据输入识别为对象数组

组件html:

<app-mycomponent
      url="https://url.com/getall" 
      data ="[{ name: 'a', required: ture},
              { name: 'b', required: false}]"
      ></app-mycomponent>

ts分量:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-mycomponent',
    templateUrl: './component.component.html',
})
export class HelloComponent {
  @Input() url: string;
  @Input() data : any;
}

1 个答案:

答案 0 :(得分:0)

您必须使用one-way data-binding [data]

<app-mycomponent
      url="https://url.com/getall" 
      [data] ="[{ name: 'a', required: true},
              { name: 'b', required: false}]">
</app-mycomponent>