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

查看:28
本文介绍了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?我试过这个",但它似乎只想要 $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.

显然,我真的不明白我在做什么,除了this 以及我在上面盲目遵循的示例,因此我向您寻求帮助.数千行 $scope-free 代码,掉到最后一关好像很丢人!

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 代替:

The solution is to use null instead:

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天全站免登陆