[英]Passing data to component angular2
Trying to pass data to a child component, I can attach the property into the template, but cant seem to access the id inside the class of the component. 尝试将数据传递给子组件时,我可以将属性附加到模板中,但是似乎无法访问组件类内部的ID。 Any ideas?
有任何想法吗?
---PARENT--- ---家长---
<related-products [id]="product.id"></related-products>
--CHILD--- - 儿童 - -
import {Component, OnInit, Input} from 'angular2/core';
import {RouteParams, Router} from 'angular2/router';
import {ProductService} from '../products/product.service.js';
@Component({
selector: 'related-products',
templateUrl: "wp-content/themes/f2/app/views/directives/related-products-template.html",
inputs: ['id'],
providers: [ProductService]
})
export class RelatedProductsComponent implements OnInit {
products: any[];
errorMessage: string;
id: number;
constructor(
private _service: ProductService,
private _router: Router,
private _routeParams: RouteParams
) {}
ngOnInit() {
console.log(this.id);
}
}
You could try to use @Input
for the id
property: 您可以尝试将
@Input
用作id
属性:
export class RelatedProductsComponent implements OnInit {
products: any[];
errorMessage: string;
@Input() // <------------
id: number;
(...)
}
but it should work with the inputs
attribute and you should have access to the value in the ngOnInit
hook method. 但它应该与
inputs
属性一起使用,并且您应该可以访问ngOnInit
挂钩方法中的值。 Are you sure that the provided expression product.id
returns something defined? 您确定提供的表达式
product.id
返回定义的值吗?
See this plunkr: https://plnkr.co/edit/aG4TdHbAls3cu04AAt64?p=preview . 请参阅以下代码: https ://plnkr.co/edit/aG4TdHbAls3cu04AAt64 ? p = preview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.