Angular-UI-Grid-Edit可以与"controller as"一起使用吗?句法? [英] Can Angular-UI-Grid-Edit be used with "controller as" syntax?

查看:72
本文介绍了Angular-UI-Grid-Edit可以与"controller as"一起使用吗?句法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了整个Angular应用程序,以避免使用$ scope,而是使用"controller as"语法来简化向Angular 2.0的过渡,因为我相信这是最新的做法.

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.

但是我无法弄清楚如何避免将$ scope与ui-grid的gridApi.edit.on.afterCellEdit事件一起使用,该事件记录如下:

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

根据我完成的文档,它可以正常工作(在将$ scope注入到控制器之后):

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 
  });
};

但是正如您所看到的,我必须在事件的回调中使用$ scope,否则我会得到一个错误消息,告诉我scope.$ on不是一个函数.是否可以不使用$ scope?我尝试过"this",但似乎只想要$ scope.

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!

预先感谢

约翰

推荐答案

解决方案是改用null:

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

从那里,您始终可以通过为其设置的变量(例如vm)访问控制器,并访问其中包含的大多数作用域.

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.

这篇关于Angular-UI-Grid-Edit可以与"controller as"一起使用吗?句法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆