Angular:afterCellEdit没有范围

时间:2016-09-28 13:02:23

标签: javascript angularjs angular-ui-grid

我正在尝试在gridOption.onREgisterApi函数中实现afterCellEdit函数。我没有在我的程序中使用$ scope,因为建议在指南中使用。

事实上,我的问题与此问题完全相同:question 可悲的是,没有回答。 当我使用null作为答案之一时,我得到了一个错误。

以下是代码:

  vm.gridOptions.onRegisterApi = function(gridApi){
    vm.gridApi = gridApi;
    vm.gridApi.edit.on.afterCellEdit(null,function(rowEntity, colDef, newValue, oldValue){
      alert("afterCellEdit");
    });
  };

这是我的错误:

typeError: Cannot read property '$on' of null
at Object.feature.on.(anonymous function) [as afterCellEdit]

谢谢!

编辑:@SaurabhTiwari这里是我的$ scope.gridData替代

function onCustomerListComplete(data) {
    vm.gridOptions.data = data;
}


function OnError(reason) {
    $log.error(reason);
}

function activate() {
  customerService.getCustomerList()
            .then(onCustomerListComplete, OnError);
}

vm.gridOptions = { 
    enablePaginationControls: false,
    useExternalSorting: true,
    enableHorizontalScrollbar : uiGridConstants.scrollbars.NEVER,
    enableVerticalScrollbar : uiGridConstants.scrollbars.WHEN_NEEDED,
  columnDefs: [
  // will be modified once the data model is set
    { field: 'id', name: '', cellTemplate: 'content/customerList/rowEditor/customerList.rowEditor.editButton.html', width: 34 },
    { name: 'customerNumber', },
    { name: 'customerName' },
    { name: 'city' },
    { name: 'creditLimit' },
    { name: 'postalCode' },
  ]
};

1 个答案:

答案 0 :(得分:0)

在这里提供一些帮助可能会迟到,你可能已经找到了解决方案。但我想用下面的代码解释你的情况。

function (scope, handler, _this) {
            if ( scope !== null && typeof(scope.$on) === 'undefined' ){
              gridUtil.logError('asked to listen on ' + featureName + '.on.' + eventName + ' but scope wasn\'t passed in the input parameters.  It is legitimate to pass null, but you\'ve passed something else, so you probably forgot to provide scope rather than did it deliberately, not registering');
              return;
            }
            var deregAngularOn = registerEventWithAngular(eventId, handler, self.grid, _this);

            //track our listener so we can turn off and on
            var listener = {handler: handler, dereg: deregAngularOn, eventId: eventId, scope: scope, _this:_this};
            self.listeners.push(listener);

            var removeListener = function(){
              listener.dereg();
              var index = self.listeners.indexOf(listener);
              self.listeners.splice(index,1);
            };

            //destroy tracking when scope is destroyed
            if (scope) {
              scope.$on('$destroy', function() {
                removeListener();
              });
            }


            return removeListener;
          }

这是来自ui-grid Api的原始代码。您可以在库中或在应用程序中的适当范围内搜索它,您可以尝试执行以下操作: $scope.gridApi.edit.on.afterCellEdit.toString()

所以这里要点是这个函数明确需要一个范围或一个null。所以你需要将一个范围传递给这个监听器。

您在问题中提到的在代码中未使用$scope的观点有些模糊。如果您有控制器,则始终关联scope。 Angular几乎是scopes(至少是Angular 1)。

参考指南所说的是你应该避免堆积$scope上的所有内容。指南还说,你应该只使用$scope来收听和观看,这就是这里的情况