0
他のコンポーネントからプロパティをバインドできません。私のコードに何か問題がありますか?角度2 |他のクラスのプロパティをバインドできません
誰でも@Input()デコレータのルールは何ですか?
ここに私の例
documentlist.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { EntryService} from '../../entry.service'
import { Entry } from '../../entry.model';
@Component({
selector: 'app-documentlist',
templateUrl: './documentlist.component.html',
styleUrls: ['./documentlist.component.css']
})
export class DocumentlistComponent implements OnInit {
@Input() entry: Entry;
dmsfile: Entry[];
constructor(private entryService: EntryService) {
}
ngOnInit() {
this.entryService.getData().then(dmsfile => this.dmsfile = dmsfile);
}
}
documentlist.component.html
<!-- start documnet list -->
<div class="documentlist">
<div class="documentlist-container">
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading clearfix">
<span class="pull-left">Document list</span>
<span class="btn btn-primary storage-new pull-right">
New
</span>
</div>
<table class="table responsive table-striped">
<thead>
<tr>
<th>Action</th>
<th>DataID</th>
<th>Storage ID</th>
<th>Owner</th>
<th>DocumentType</th>
<th>Ref No.</th>
<th>Status</th>
<th>Created by</th>
<th>CreatedDate</th>
<th>Modified by</th>
<th>ModifiedDate</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let documentlist of dmsfile" [entry]="documentlist">
<td>
<p>
<span class="glyphicon glyphicon-trash"></span>
<span class="glyphicon glyphicon-eye-open"></span>
</p>
</td>
<td *ngFor="let dataids of documentlist.dataid"><p>{{ dataids.id }}</p></td>
<td *ngFor="let storageids of documentlist.storageid"><p>{{ storageids.id }}</p></td>
<td *ngFor="let ownerids of documentlist.storageid"><p>{{ ownerids.ownerid }}</p></td>
<td><p>{{ documentlist.documenttype }}</p></td>
<td><p>{{ documentlist.documentreferenceno }}</p></td>
<td><p>{{ documentlist.documentstate }}</p></td>
<td><p>{{ documentlist.createdby }}</p></td>
<td><p>{{ documentlist.createddate }}</p></td>
<td><p>{{ documentlist.modifiedby }}</p></td>
<td><p>{{ documentlist.modifieddate }}</p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- end document list -->
entry.model.ts
export class Entry {
dataid: [
{
dataid: [
{
id: number,
title: string,
comment: string,
releasedate: string,
releaseversion: string,
resourcetcode: string,
resourcetname: string,
createdby: string,
createddate: string
}
],
storageid: [
{
id: number,
ownerid: number,
room: string,
cabinet: string,
bin: string,
description: string,
storagetype: string,
islock: boolean,
createdby: string,
createddate: string
}
],
documenttype: string,
documentreferenceno: string,
description: string,
documentstate: string,
profile: string,
createdby: string,
createddate: string,
modifiedby: string,
modifieddate: string
}
]
}
「他のコンポーネントからプロパティをバインドできません」という意味はどうですか?あなたは@Inputを使ってデータをコンポーネントに渡します。 – crash
はい、私は、documentlist.component '@Input()entry:Entry;' ** entry.model.ts **からEntryクラスを呼び出しています。それを使用しています。** documentlist.component.html ** '
もちろん、 'entry'は' DocumentlistComponent'コンポーネントとhtml要素 '
答えて
は、一般的にはです、そこにはあるあるコンポーネントから別のコンポーネントにデータを渡すことができます。あなたが従うべきアプローチは、主にコンポーネント間の関係に依存します。
@Input()
コンポーネント間の親子関係は、例えば、そこにある: の2つのコンポーネント
parent-component
とchild-component
はのテンプレートコードで今があるとしましょう
inputData
がに渡されることをここお知らせ -
parent-component
、あなたのコードは次のようになりますchild-component
。あなたが推測したように、右側のinputData
はparent-component
から設定し、[inputData]
はone way data-bindingであることを示します。コンポーネントクラスの
child-component
かもしれない:私たちは@Input()
としてinputData
を渡されたのでので、我々は
child-component
でそれを手に入れる必要があります -parent-component
のためのあなたのコンポーネントクラスは次のようになります。このように見える -あなたのテンプレートコードでと、それは常に別のような1つのコンポーネントからのデータを渡すために他の方法があるコンポーネント間の関係に応じて、
parent-component
から渡された更新された値が表示されます - 持つEventEmitterと共有サービスを 。
これが役に立ちます。
出典
2017-06-29 06:34:54 Abrar
これは、子コンポーネントが親コンポーネントのデータを保持し、子コンポーネントが変更の親コンポーネントをリッスンすることを意味しますか? –
はい、あなたは正しいです – Abrar
これはありがとう:)私は私のコンポーネントの構造に混乱しているようです。最後の一人。 @Inputデコレータは、親子関係のコンポーネント構造がある場合にのみ有効です。 –
関連する問題