[英]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.