I have created wrapper directive over ag grid as below
function MyDirective(): ng.IDirective {
var directive = <ng.IDirective>{
restrict: "E",
template: '<div style="width: 100%; height: 400px;" ag-grid="vm.agGridOptions" class="ag-fresh ag-basic"></div>',
scope: { gridOptions: '=', rowClicked: "&", api: '=' },
controller: gridController,
controllerAs: 'vm',
bindToController: true
}
return directive;
}
angular.module("angularWithTS").directive("myDirective", MyDirective);
My controller class looks like below:
export class gridController {
public agGridOptions: any = {};
public api: any = {};
public gridOptions: any;
public rowClicked: any;
constructor() {
this.Process();
}
private Process() {
/*****Contoller Logic*****/
var columnDefs = commonFunctions.convertToAgColumns(this.gridOptions.columnDefinitions);
this.agGridOptions.enableSorting = true;
this.agGridOptions.editable = true;
this.agGridOptions.enableColResize = true;
this.agGridOptions.columnDefs = columnDefs;
/*****Contoller Logic*****/
/*****Exposed Events*****/
this.agGridOptions.onRowClicked = function (event) {
this.rowClicked({ index: event.rowIndex });
};
/*****Exposed Events*****/
/*****Public Api*****/
this.api = {
populateData: function (options) {
this.agGridOptions.api.setRowData(options.rowData);
}
}
}
/*****Public Api*****/
}
My directive tag in html looks like below
<my-directive grid-options="options" api="gridApi"></my-directive>
Question: When i tries to call api method populateData() in controller scope variables like agGridOptions is undefined and then rest is not working. Why variable agGridOptions is not available when i call public api ? Please help.. its working fine when i code normal js functions way controller but not working with typescript class controller. Any help would be appreciated
I m calling controller method like below:
$scope.gridApi = {};
$scope.options = {};
$scope.options.columnDefinitions = $scope.columnDefinitions;
$http.get("monthlySales.json").then(function (response) {
$timeout(function () {
$scope.options.rowData = response.data;
$scope.gridApi.populateData($scope.options);
},2000);
});
When controller invoked first time all the values of variables in controller like gridOptions,agGridOptions are properly get. But agGridOptions getting undefined when i call api populateData to show fetched data.