2017-01-23 6 views
0

У меня проблема с настройкой свойства ввода. То, что я пытаюсь сделать, это передать значение из app.component.ts под названием passBool и установить свойство of nextComponent под названием полученоBool.@ Вход не работает Угловой 2

Вот мои коды:

app.component.ts

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <nextComponent [receivedBool]="passBool"></nextComponent> 
    ` 
}) 

export class AppComponent { 

    //Variables 
    passBool: Boolean = true; 

    constructor(){ 
    console.log('The boolean value we are trying to pass is: ' + this.passBool) 
    } 

} 

nextComponent.component.ts

import { Component, Input } from '@angular/core'; 

@Component({ 
    selector: 'nextComponent', 
    template: `<i> </i> ` 
}) 

export class NextComponent { 

    @Input() receivedBool: Boolean = false; 

    constructor() { 
     console.log('The boolen value we are receiving here is: ' + this.receivedBool) 
    } 

} 

результаты журнала консоли являются:

The boolean value we are trying to pass is: true - app.component.ts

The boolean value we are receiving here is: false - nextComponent.component.ts

Я надеюсь, что вы могли бы просветить меня. Благодаря!

ответ

6

Входы еще не доступны при выполнении конструктора.

Использование ngOnInit() вместо:

export class NextComponent { 
    @Input() receivedBool: Boolean = false; 

    ngOnInit() { 
     console.log('The boolen value we are receiving here is: ' + this.receivedBool) 
    } 
} 
+0

Спасибо, это помогло мне фигурный остальное вне! –

+0

Угловая не считывает значение атрибута во время выполнения только во время компиляции, проверьте этот ответ https://stackoverflow.com/questions/39614451/angular-2-input-binding-does-not-work/39614592#39614592 – kelgwiin

+0

@kelgwiin что ваш комментарий должен указывать? –

Смежные вопросы