如何从外部访问UI-Grid隔离范围?

时间:2016-02-09 19:45:09

标签: angularjs angularjs-scope angular-ui-grid

角度UI网格外面有一个按钮。 我想在“默认”按钮上调用一个函数点击并将网格对象参数发布到回调函数。

还有第二个例子。但是,我不想将网格对象发布到网格内的按钮,而是将网格对象发布到网格外部的按钮。

简而言之,我想在网格外部设置一个编辑按钮(选择一行模式已打开),而不是添加一列编辑按钮而不选择行选项。

是否可以从v3.1.0开始? init(string:attributes:)

由于某些原因,当我在MainCtrl中展开$ scope对象时,gridOptions.appScopeProvider为null。这是js-code sample:

angular.module('modal.editing', ['ui.grid', 'ui.grid.selection', 'ui.grid.edit', 'ui.bootstrap', 'schemaForm'])

.constant('PersonSchema', {
  type: 'object',
  properties: {
    name: { type: 'string', title: 'Name' },
    company: { type: 'string', title: 'Company' },
    phone: { type: 'string', title: 'Phone' },
    'address.city': { type: 'string', title: 'City' }
  }
})

.controller('MainCtrl', MainCtrl)
.controller('RowEditCtrl', RowEditCtrl)
.service('RowEditor', RowEditor)
;

MainCtrl.$inject = ['$http', 'RowEditor', '$modal'];
function MainCtrl ($http, RowEditor) {
  var vm = this;

  vm.editRow = RowEditor.editRow;

  vm.gridOptions = {
    enableRowSelection: true,
    enableRowHeaderSelection: false,
    multiSelect: false,
    selectionRowHeaderWidth: 35,
    columnDefs: [
      { field: 'id', name: '', cellTemplate: 'edit-button.html', width: 34 },
      { name: 'name' },
      { name: 'company' },
      { name: 'phone' },
      { name: 'City', field: 'address.city' },
    ]
  };

  vm.test = function() {
    debugger;
  };

  $http.get('http://ui-grid.info/data/500_complex.json')
    .success(function (data) {
      vm.gridOptions.data = data;
    });
}

RowEditor.$inject = ['$rootScope', '$modal'];
function RowEditor($rootScope, $modal) {
  var service = {};
  service.editRow = editRow;

  function editRow(grid, row) {
    debugger;
    $modal.open({
      templateUrl: 'edit-modal.html',
      controller: ['$modalInstance', 'PersonSchema', 'grid', 'row', RowEditCtrl],
      controllerAs: 'vm',
      resolve: {
        grid: function () { return grid; },
        row: function () { return row; }
      }
    });
  }

  return service;
}

function RowEditCtrl($modalInstance, PersonSchema, grid, row) {
  var vm = this;

  vm.schema = PersonSchema;
  vm.entity = angular.copy(row.entity);
  vm.form = [
    'name',
    'company',
    'phone',
    {
      'key': 'address.city',
      'title': 'City'
    },
  ];

  vm.save = save;

  function save() {
    // Copy row values over
    row.entity = angular.extend(row.entity, vm.entity);
    $modalInstance.close(row.entity);
  }
}

1 个答案:

答案 0 :(得分:0)

这正是onRegisterApi的用途:

      vm.gridOptions = {
      enableRowSelection: true,
      enableRowHeaderSelection: false,
      multiSelect: false,
      selectionRowHeaderWidth: 35,
      columnDefs: [
        { field: 'id', name: '', cellTemplate: 'edit-button.html', width: 34 },
        { name: 'name' },
        { name: 'company' },
        { name: 'phone' },
        { name: 'City', field: 'address.city' },
      ],
      onRegisterApi: function (gridApi){
           vm.gridApi = gridApi;
      }
      };

然后gridApi将在你的范围内(vm.gridApi),你可以访问gridApi.grid中的网格和所有行。

希望这有帮助。