ng-click not worked inside ag-grid cell

杀马特。学长 韩版系。学妹 提交于 2021-01-27 10:26:34

问题


i am working with ag-grid and i need to add some custom links in a cell and want to call ng-click function on it. Here is my code

var columnDefs =[
                {headerName: "ID", field: "id"},
                {headerName: "Template Name", field: "user_template_name"},
                {headerName: "Screen", field: "screen_name"},
                {headerName: "Last Uploaded", field: "created_at"},                            
                {headerName: "Manage", cellRenderer: createCustomLinks }
              ];
  var gridOptions = {
                        angularCompileRows:true,
                        columnDefs: columnDefs,
                        rowData: null,
                        enableSorting: true,
                        enableColResize: true,
           };
 var gridDiv = document.querySelector('#myGrid');
 new agGrid.Grid(gridDiv, gridOptions);             
 gridOptions.columnApi.setColumnVisible('id', false);
 gridOptions.api.sizeColumnsToFit();

 function createCustomLinks(params) {
            var cellHtml = '<a ng-click="openTemplateID('+params.data.id+')">Open</a>';

            var domElement = document.createElement("span");
                domElement.innerHTML = cellHtml;

               params.$scope.openTemplateID = function(id){
                  console.log(id);
               }

            return domElement;
          };

i got the following error

TypeError: _this.$scope is null
setTimeout(function () { _this.$scope.$apply(); }, 0);  
ag-grid.js (line 7415, col 39)
Error: this.parentScope is null
RenderedRow</RenderedRow.prototype.createChildScopeOrNull

Please guide me if i am doing something wrong. Thank you.


回答1:


I had this issue once. We need to define it in the grid options. Setting angularCompileRows to true resolved this issue for me

$scope.gridOptions = {
        columnDefs: $scope.columnDefs,
        rowData: $scope.rowData,
        // all other options 
        angularCompileRows: true
    };



回答2:


When using ag-grid along with angularJS don't do :

 var gridDiv = document.querySelector('#myGrid');
 new agGrid.Grid(gridDiv, gridOptions);   

Instead use in your HTML

<div ag-grid="gridOptions"></div> // don't forget having grdOptions bind to $scope

This alone will trigger you a new problem : columnApi and api will be undefined at the time of execution. To fix this you will have to use the onGridReady callback :

gridOptions.onGridReady = function(params){
     gridOptions.columnApi.setColumnVisible('id', false);
     gridOptions.api.sizeColumnsToFit();
}


来源:https://stackoverflow.com/questions/37341440/ng-click-not-worked-inside-ag-grid-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!