Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope Variable undefined in angularjs typescript controller class of directive

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.

like image 554
Kiran Saravade Avatar asked Jun 13 '26 13:06

Kiran Saravade


1 Answers

The 'this' you are calling to in your function is referring to the function itself, and not your controller-

this.api = {
        populateData: function (options) {
            this.agGridOptions.api.setRowData(options.rowData); //(this = populateData function)
        }
    }

The syntax should be changed to () => (this will make typescript compiler to 'transfer' the this, it will become _this in the js file)

It should look like this:

this.api.populateData =  (options)=> {
            this.agGridOptions.api.setRowData(options.rowData);    
    }
like image 136
Ziv Weissman Avatar answered Jun 15 '26 03:06

Ziv Weissman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!