5

我想创建一个带有链接的自定义列,并在 ng-click 上调用 $scope 方法。ngGrid 有一个非常相似的问题(如何从 ngGrid -in Angular js 中显示的按钮调用范围方法)并且该解决方案有效。我正在使用 ui-grid,它应该只是 ngGrid 的更新版本,但它似乎在那里不起作用。

这是我的代码:

var app = angular.module('plunker', ['ui.grid']);

app.controller('MainCtrl', function($scope) {  
  $scope.gridOptions = {
    data: [{name: 'test'}],
      columnDefs: [{
        field:'name', 
        displayName:'name', 
        cellTemplate: '<div ng-click="gridOptions.editUser()">Edit</div>'
      }],
      editUser: $scope.editUser
    };

  $scope.editUser = function() {
    alert('It works!');
  };
});

http://plnkr.co/edit/Q5SuIeAPFpZaUKbmIDCn

这是适用于 ngGrid 的原始解决方案:http: //plnkr.co/edit/hgTQ1XdEVRyxscoNs76q

4

2 回答 2

4

在新版本的 ui-grid 中,这种方法对我不起作用。相反,我使用了 appScope 核心 API。 http://ui-grid.info/docs/#/tutorial/305_appScope

于 2015-05-17T07:12:46.033 回答
3

您应该使用属性声明将在单元格模板中使用的外部范围:external-scopes

<div ui-grid="gridOptions" class="grid" external-scopes="gridScope"></div>

然后在控制器中你需要定义这个范围对象:

$scope.gridScope = {
    editUser: function() {
        alert('It works!');
    }
}

最后你在模板中访问这个外部范围

cellTemplate: '<div ng-click="getExternalScopes().editUser()">Edit</div>'

演示:http ://plnkr.co/edit/saYe6sddbcO76o9qBrNu?p=preview

于 2014-12-23T22:33:31.083 回答