javascriptangulartypescriptangular-input

Why Angular Input is undefined?


I have typical easy situation but in that case @Input() does not working...

Child Component:

@Component({
  selector: 'app-test-grid',
  templateUrl: './test-grid.component.html',
  styleUrls: ['./test-grid.component.scss'],
})
export class TestGridComponent implements OnInit {
  @Input() memos: any[];

  constructor() {
    console.log(this.memos); // prints undefined
  }

  ngOnInit() {
    console.log(this.memos); // also prints undefined
  }
}
<div *ngIf="checker()">
  THIS IS DISPLAYED
  <app-test-grid>
    [memos]="test"
  </app-test-grid>
</div>

// parent component.ts
  test: number[] = [1, 2, 3, 4, 5, 6, 7];

  ngOnInit() {
    console.log(this.test); // print [1,2,3,4,5,6,7]
  }

 checker() {
    console.log('checker', this.test.length !== 0); // this prints true

    return this.test.length !== 0;
  }

what's wrong with this code? this is very rudimentary case... I tried to found some another posts related but I didn't found answer.


Solution

  • You closed the app-test-grid tag so the memos assignment is lost as the content of the component. Use this:

    <app-test-grid [memos]="test">
    </app-test-grid>