私はこれに関するすべてのフォーラムを読みましたが、まだこれを機能させることができません。親コンポーネントが 1 つあり、その中にいくつかの子コンポーネントをロードしたい (それぞれに異なる入力パラメーターを渡す)。
私のアプリケーションは次のようにセットアップされています:
- 私のapp.componentには、ヘッダー、コンテンツ(まだ実装していません)、およびフッターの3つのサブコンポーネントがあります。
- ヘッダー コンポーネントで繰り返す必要がある別のコンポーネントがあるため、子コンポーネントを作成しました (ヘッダー コンポーネント内でのみ使用します)。
以下はすべての私のコードです:
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { ChildComponent } from './child/child.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
ChildComponent
],
imports: [
BrowserModule,
HttpModule
],
providers: [
],
bootstrap: [
AppComponent,
HeaderComponent,
FooterComponent,
ChildComponent
]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<header class="container-fluid">
<app-header></app-header>
</header>
<content class="container-fluid">
</content>
<footer class="container-fluid">
<app-footer></app-footer>
</footer>`
})
export class AppComponent { }
header.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-header',
template: `
<div>
<h2>HEADER</h2>
<app-child [code]="'1st value'"></app-child><br>
<app-child [code]="'2nd value'"></app-child><br>
<app-child [code]="'3rd value'"></app-child>
</div>`
})
export class HeaderComponent { }
footer.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-footer',
template: `<div><h2>FOOTER</h2></div>`
})
export class FooterComponent { }
child.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `<div>Hello World! {{this.code}}</div>`
})
export class ChildComponent {
@Input() code: string;
}
しかし、何らかの理由で、バインディングは最初の子コンポーネントでは機能しません。出力は次のとおりです。
Hello World!
Hello World! 2nd value
Hello World! 3rd value
さらに紛らわしいことに、フッター コンポーネントを削除すると、これは機能しますが、フッター コンポーネントは、ヘッダー コンポーネントまたはその子コンポーネントとはまったく関係がありません。
これが最初の発生でのみ失敗し、他のすべてのバインディングでは正常に機能する理由を誰かが理解するのを手伝ってくれますか?
角度特性:
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.2.1
node: 6.10.0
os: win32 x64
@angular/animations: 4.2.6
@angular/common: 4.2.6
@angular/compiler: 4.2.6
@angular/compiler-cli: 4.2.6
@angular/core: 4.2.6
@angular/forms: 4.2.6
@angular/http: 4.2.6
@angular/platform-browser: 4.2.6
@angular/platform-browser-dynamic: 4.2.6
@angular/platform-server: 4.2.6
@angular/router: 4.2.6
@angular/cli: 1.2.1
@angular/language-service: 4.2.6
助けてくれてありがとう。