Angular 角度2 |我能';不能从另一个类绑定属性
我无法从其他组件绑定属性。我的密码有问题吗 还有谁能解释一下@Input()decorator的规则是什么 这是我的例子 documentlist.component.tsAngular 角度2 |我能';不能从另一个类绑定属性,angular,Angular,我无法从其他组件绑定属性。我的密码有问题吗 还有谁能解释一下@Input()decorator的规则是什么 这是我的例子 documentlist.component.ts import { Component, OnInit, Input } from '@angular/core'; import { EntryService} from '../../entry.service' import { Entry } from '../../entry.model'; @Component
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 -->
一般来说,有几种方法可以将数据从一个组件传递到另一个组件。您应该遵循的方法主要取决于组件之间的关系 @Input() 组件之间存在父子关系时,例如: 假设有两个组件
父组件
和子组件
现在,在父组件的模板代码中,您的代码可能如下所示-
<!-- more code here --->
<div>
<child-component [inputData]="inputData"> </child-component>
</div>
export class ParentComponent {
//more code here
public inputData = "Input Value From Parent Component";
}
import {Input, SimpleChanges, OnChanges} from '@angular/core';
//more import statements here
export class ChildComponent{
@Input() inputData: any;
public myInputData: any;
ngOnChanges(changes : SimpleChanges){
if('inputData' in changes){
this.myInputData = changes['inputData'].currentValue;
}
}
}
由于我们将inputData
作为@Input()
传递,因此我们必须在子组件中获得它:
子组件的组件类可能如下所示-
<!-- more code here --->
<div>
<child-component [inputData]="inputData"> </child-component>
</div>
export class ParentComponent {
//more code here
public inputData = "Input Value From Parent Component";
}
import {Input, SimpleChanges, OnChanges} from '@angular/core';
//more import statements here
export class ChildComponent{
@Input() inputData: any;
public myInputData: any;
ngOnChanges(changes : SimpleChanges){
if('inputData' in changes){
this.myInputData = changes['inputData'].currentValue;
}
}
}
现在,您可以在模板代码中显示myInputData
,它将始终显示从父组件传递的更新值
根据组件之间的关系,还有其他方法可以将数据从一个组件传递到另一个组件,如-EventEmitter和共享服务
希望这能有所帮助。你说的“我不能从其他组件绑定属性”是什么意思?您将使用@Input将数据传递给组件是的,请查看documentlist.component@Input()条目:条目
我从Entry.model.ts调用Entry类,并在documentlist.component.html
中使用它,但我仍然得到无法绑定到“Entry”,因为它不是“tr”的已知属性。
,entry
是在DocumentlistComponent
组件和html元素
@JydonMah中定义的输入属性,我在下面添加了一个详细的答案。请看一看是否有用。这意味着子组件保存父组件的数据,子组件侦听父组件的更改?谢谢:)我似乎对组件的结构感到困惑。最后一件事,先生。因此,@Input decorator只有在您具有父子关系组件结构时才有效?只要您有一个组件实例,您希望从该组件中接收数据,并从该组件中发送数据(感谢Abrar获得了它:)