简体   繁体   English

离子2:当我将数组传递给组件时,我在component.ts上得到一个字符串

[英]Ionic 2: when I pass array to component I get a string on component.ts

I have a problem when I try to to pass an array to a component trough an input. 当我尝试通过输入将数组传递给组件时遇到问题。 I console.log the array on home.ts and i get is an object, and then, when i pass it to the component, on component.ts i get it is a string. 我在home.ts上console.log数组,我得到的是一个对象,然后,当我将其传递给组件时,在component.ts上,我得到的是一个字符串。

I dont know if i do it well 我不知道我做得好吗

On component ts I have this: 在组件ts上我有这个:

@Component({
  selector: 'micomponente',
  templateUrl: 'micomponente.html'
})
export class MicomponenteComponent {
    @Input() pra:any=[];
  text: string;

  constructor() {
    console.log('Hello MicomponenteComponent Component');
    this.text = '';
  }

   ngOnInit() {
    console.log(typeof(this.pra)) //this is the red arrow on the picture
    this.text = this.pra;
  }

}

home.html home.html

<ion-content padding>
 <micomponente pra="{{algo}}"></micomponente>
</ion-content>

home.ts home.ts

export class HomePage {
algo:any=[];

  constructor(public navCtrl: NavController) {
    this.algo.push(['1','fsa']);
    this.algo.push(['2','fsd']);
    console.log(this.algo)

  }

}

and this is console log 这是控制台日志

控制台日志映像

To pass data to a child component the property should be put in brackets [] and property should be in quotes. 要将数据传递给子组件,应将属性放在方括号[]中,并将属性括在引号中。 In your example the input data should be passed like this: 在您的示例中,应按以下方式传递输入数据:

<ion-content padding>
    <micomponente [pra]="algo"></micomponente>
</ion-content>

See https://angular.io/guide/component-interaction for how to pass data to and from a child component. 有关如何在子组件之间传递数据,请参见https://angular.io/guide/component-interaction Double curly braces are used for interpolation, to display properties as string in html. 双花括号用于插值,以将属性显示为html中的字符串。 Please read here for more information about interpolation: https://angular.io/guide/template-syntax#interpolation---- . 请阅读此处以获取有关插值的更多信息: https : //angular.io/guide/template-syntax#interpolation----

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要关闭angular4中component.ts中的弹出消息 - I need to close the popup message in component.ts in angular4 Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file? 为什么每当我在我的 angular component.ts 上启用旋转动画时,我的应用程序就会变成空白? - Why is it that whenever i enable a rotate animation on my angular component.ts my application becomes blank? 来自component.ts的* ngFor访问元素 - Access element of *ngFor from component.ts 无法将 js 文件导入 component.ts - Could not import js file into component.ts 如何在angular的component.ts文件中传递复选框的选中和未选中值? - How to pass the checked and unchecked value of checkboxes in component.ts file in angular? 如何将变量从一个函数传递到同一component.ts中的另一个函数 - how to pass variable from one function to another function in same component.ts in angular 6 如何在component.ts中将数据从一个函数传递到另一个函数,angular2 - how to pass data from one function to another in component.ts , angular2 如何将index.html页面中的值传递给angular4中的component.ts文件 - How to pass a value from index.html page to component.ts file in angular 4 如何将角度从component.ts传递给html SVG - How to pass a class name in angular from component.ts to html SVG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM