Can Angular-UI-Grid-Edit be used with “controller as” syntax?

核能气质少年 提交于 2019-12-10 14:59:57

问题


I've written my entire Angular app managing to avoid the use of $scope, using the "controller as" syntax instead, to help ease the transition to Angular 2.0 and because I believe this is the more current practice.

But I can't figure out how to avoid using $scope with ui-grid's gridApi.edit.on.afterCellEdit event, which is documented as follows:

gridApi.edit.on.afterCellEdit(scope,function(rowEntity, colDef){})

Parameters

rowEntity – {object} – the options.data element that was edited
colDef – {object} – the column that was edited
newValue – {object} – new value
oldValue – {object} – old value

Based on the documentation I have done this, which works (after injecting $scope into the controller):

ctrl.tableOptions.onRegisterApi = function(gridApi){
  ctrl.gridApi = gridApi;
  gridApi.edit.on.afterCellEdit( $scope, function(rowEntity,colDef, newValue, oldValue) {
    //save stuff here 
  });
};

but as you can see I had to use $scope in the event's callback, otherwise I get an error telling me scope.$on is not a function. Is it possible to NOT use $scope? I tried "this" but it only seems to want $scope.

Obviously, I don't really understand what I am doing, and I can't find much documentation on afterCellEdit other than this and the example which I slavishly followed above, so I am turning to you for help. Thousands of lines of $scope-free code, it seems like a shame to fall at the last hurdle!

thanks in advance

John


回答1:


The solution is to use null instead:

gridApi.edit.on.afterCellEdit(null,function(rowEntity, colDef, newValue, oldValue)

From there you can always access the controller through the variable you set for it e.g vm and access most of what scope would contain.




回答2:


I had the same problem (...avoid using $scope with ui-grid's gridApi...) and did a basic app on plunker with ui-grid pagination. I hope this helps. http://plnkr.co/edit/sQos8J?p=preview

ng-click="main.gridApi2.pagination.previousPage()


来源:https://stackoverflow.com/questions/29114156/can-angular-ui-grid-edit-be-used-with-controller-as-syntax

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