简体   繁体   English

如何正确地以角度2将数据从组件传递到另一个组件

[英]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.

相关问题 如何在 Angular 中将数据从一个组件传递到另一个组件? - How do I pass data from one component to another in Angular? 如何通过路由器将数据从角度组件传递到另一个组件? - How do I pass data from angular component to another component via router? 如何以角度将数据从一个组件传递到另一个组件(新浏览器选项卡)? - How do I pass data from one component to another (New Browser Tab) in angular? 如何在 Angular 中将数据从一个组件发送到另一个组件? - How do I send data from a Component to another Component in Angular? Angular - 如何将数据从一个组件传递到另一个 - Angular - how to pass data from a component to another 如何将角度2中的数据从一个组件传递到另一个组件? - how to pass data from one component to another component in angular 2? 如何在单击按钮时将 id 和对象从一个组件传递到另一个组件的 angular 函数? - How do I pass an id and object on click of a button from one component to another component' s function in angular? 如何在 Angular 中将数据或值从一个组件传递到另一个组件 - How to Pass Data or values From One Component to another in Angular 如何将数据从一个组件传递到另一个 angular2 - How to Pass the data from one component to another angular2 如何以角度 9 将表数据从一个组件推送/传递到另一个组件? - How to push/pass table data from one component to another in angular 9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM