@Input访问从数据库加载的对象

时间:2016-11-23 23:00:18

标签: angular

如何使用子组件本身中的@Input()从父组件访问对象?

如果我从数据库加载它,我就无法获得对象。但是我可以访问子模板中的对象。

UPDATE:templateUrl to ParentComponent

中的模板
import { Component, OnInit, Input } from '@angular/core';
@Component({
   moduleId: module.id,
   selector: 'child-component',
   templateUrl: './child-component.component.html'
})
export class ChildComponent implements OnInit {
   @Input object: Object; // return object in template / undefined in component if loaded from database

   ngOnInit(){
      console.log(this.object); // return undefined if object is loaded from database
   }
}

import { Component, OnInit } from '@angular/core';
...
@Component({
   moduleId: module.id,
   selector: 'parent-observation',
   template: `<child-component [object]="object"></child-component>`,
})
export class ParentComponent implements OnInit {
   object: Object;

   constructor(private service: Service){}

   ngOnInit(){
     /** 
      this.service.get(id)
          .subscribe((data:Object) => {
              console.log(data); // return object
              this.object = data;
          }); // this works but console.log(this.object) returns undefined
     **/

      this.object = {prop1: 'prop1', prop2: 'prop2'}; // this works and console.log(this.object) returns object
   }
}

0 个答案:

没有答案