Я создаю приложение углового2. У меня есть дочерний компонент (MountainListComponent), который состоит из других компонентов (но это не важно), которые включены в родительский компонент. В дочернем компоненте у меня есть список гор (<my-mountain class="col-md-4" *ngFor="#mountain of mountains" [mountain]="mountain" (click)="onChange()"></my-mountain>
), когда я нажимаю на гору, я бы хотел передать логическую переменную (hidelist
) в родительский компонент.Вход и выход в угловой2
Код этих двух компонентов приведен ниже. Я удалил шаблонный код.
Родитель компонент:
import {Component, Input} from "angular2/core";
import {MountainListComponent} from "./mountain-list.component";
@Component({
selector: 'my-mountains',
template: `hidelist value: {{hidelist}}
<div class="row spacing" (childChanged)="hidelist=$event" *ngIf="!hidelist">
<my-mountain-list></my-mountain-list>
</div>
`
})
export class MountainsComponent {
hidelist = false;
}
Детский компонент:
import {Component, Output, EventEmitter} from "angular2/core";
import {MountainComponent} from "./mountain.component";
import {Mountain} from "./mountain";
@Component({
selector: 'my-mountain-list',
template: `
<section class="col-md-12">
<my-mountain class="col-md-4" *ngFor="#mountain of mountains" [mountain]="mountain" (click)="onChange()"></my-mountain>
</section>
`,
directives: [MountainComponent],
outputs: ['childChanged']
})
export class MountainListComponent implements OnInit {
childChanged = new EventEmitter<boolean>();
onChange() {
var hidelist: boolean;
hidelist = true;
this.childChanged.emit(hidelist);
}
}
Проблема заключается в том, что hidelist
значение переменной всегда ложно