[英]How do I properly pass data from a component to another in angular 2
I have these codes: 我有以下代码:
moduleOne.component.ts: moduleOne.component.ts:
@Component({
selector: 'one',
templateUrl: './moduleOne.component.html'
})
export class ModuleOneComponent {
private ag: AgGridModel;
private columnDefs = [
{headerName: "Sport", field: "sport", width: 110, suppressMenu: true},
{headerName: "Gold", field: "gold", width: 100, suppressMenu: true},
{headerName: "Silver", field: "silver", width: 100, suppressMenu: true},
{headerName: "Bronze", field: "bronze", width: 100, suppressMenu: true},
{headerName: "Total", field: "total", width: 100, suppressMenu: true}
];
constructor(){
this.ag.id = "one";
this.ag.tableName = "module one";
this.ag.colDefs = this.columnDefs;
// Define delegate button
var delegateBtn = new buttonModel(
'delegateId',
'Delegate',
'glyphicon icon-arrow_right',
this.delegateJob
) ;
// Define print button
var printBtn = new buttonModel(
'printId',
'Print',
'glyphicon print',
this.printJob
);
this.ag.gridButtons.push(printBtn);
this.ag.gridButtons.push(delegateBtn);
}
private delegateJob() {
// TODO: call delegateModal
return;
}
private printJob() {
// TODO: call delegateModal
return;
}
}
moduleOne.component.html: moduleOne.component.html:
<div class="container-fluid single-col-full-width-container">
<ag-grid-common [ag]="ag" ></ag-grid-common>
</div>
ag-grid-common.component.ts: ag-grid-common.component.ts:
@Component({
selector: 'ag-grid-common',
templateUrl: './ag-grid-common.component.html'
})
export class AgGridCommonComponent implements OnInit {
@Input() public ag: AgGridModel;
private gridOptions:GridOptions;
private rowCount;
ngOnInit(){
this.gridOptions = this.ag.gridOptions;
// Dynamically create ag-grid in the selected element
var idSelector = '#' + this.ag.id;
var gridDiv = <HTMLInputElement>document.querySelector(idSelector);
new Grid(gridDiv, this.gridOptions);
this.createNewDatasource();
}
constructor() {
}
}
ag-grid-common.component.html: ag-grid-common.component.html:
<div style="width: 100%">
<div class="row">
<div class="col-sm-12">
<div class="col-sm-1" *ngFor="let btn of ag.gridButtons">
<cui-button [id]="btn.id" [iconClass]="btn.icon" (click)="btn.callbackFunc">{{btn.name}}</cui-button>
</div>
</div>
</div>
<div style="height: 320px; padding-top: 10px;" [id]="ag.id" class="ag-fresh"></div>
The ag variable of ag-grid-common is an input variable from moduleOne and some attributes of the ag variable are used in ag-grid-common.component.html ag-grid-common的ag变量是来自moduleOne的输入变量,并且ag-grid-common.component.html中使用了ag变量的某些属性。
I got this error: ag-Grid: no div element provided to the grid 我收到此错误:ag-Grid:没有为网格提供div元素
When I check the page source, all directives with ag are not loaded. 当我检查页面源代码时,不会加载所有带有ag的指令。
here is the page source: 这是页面来源:
<ag-grid-common ng-reflect-ag="[object Object]">
<br>
<div style="width: 100%">
<div class="row">
<div class="col-sm-12">
<!--template bindings={}-->
**<div class="col-sm-1">**
</div>
</div>
</div>
<div style="padding: 4px; width: 100%; ">
<div style="height: 6%;">
Page Size:
<select>
<option selected="" value="10">10</option>
<option value="100">100</option>
<option value="500">500</option>
<option value="1000">1000</option>
</select>
</div>
<div class="ag-fresh" style="height: 320px; padding-top: 10px;"></div>
<br></div>
</div>
</ag-grid-common>
In AgGridCommonComponent.ngOnInit()
you query the DOM but the DOM is probably not available yet. 在AgGridCommonComponent.ngOnInit()
您查询DOM,但DOM可能尚不可用。 This code should be moved to ngAfterViewInit()
此代码应移至ngAfterViewInit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.