我@input
用来从父组件接收属性,以便在子组件的元素之一中激活CSS类。
我可以从父级接收属性,也可以激活该类。但这仅工作一次。我从父级收到的属性是一个布尔数据类型,当我将其状态设置为false
子级组件时,它在父级中不会更改。
Plunkr:https://plnkr.co/edit/58xuZ1uzvToPhPtOING2 ? p = preview
应用程序
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { HeaderComponent } from './header';
import { SearchComponent } from './header/search';
@Component({
selector: 'my-app',
template: `
<app-header></app-header>
`,
})
export class App {
name:string;
constructor() {
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, HeaderComponent, SearchComponent ],
bootstrap: [ App ]
})
export class AppModule {}
标头
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-header',
template: `<header>
<app-search [getSearchStatus]="isSearchActive"></app-search>
<button (click)="handleSearch()">Open Search</button>
</header>`
})
export class HeaderComponent implements OnInit {
isSearchActive = false;
handleSearch() {
this.isSearchActive = true
console.log(this.isSearchActive)
}
constructor() { }
ngOnInit() { }
}
标头/search.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-search',
template: `<div id="search" [class.toggled]="getSearchStatus">
search
<button (click)="getSearchStatus = false" class="close">Close Search</button>
</div>`
})
export class SearchComponent implements OnInit {
@Input() getSearchStatus: boolean;
constructor() { }
ngOnInit() {
}
}
请检查以上给定的矮人。打开搜索功能仅工作一次。关闭搜索后,它不会再次触发。
@input
这种情况是否合适?请帮我解决这个问题。(请更新活塞)。
true
in后,您在哪里设置“ this.isSearchActive = false;”handleSearch()
?