如何使用 $scope 变量在 celltemplate 中的 ui-grid 中使用 ng-show

How to use ng-show in ui-grid within celltemplate using $scope variable

我正在尝试显示基于范围变量的元素。我似乎无法让它工作。由于 $scope.hi 存在,我希望 "Active" 这个词出现。这是我的代码:

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

app.controller('MainCtrl', function ($scope, $timeout) {
  $scope.hi = true;
  $scope.gridOpts = {        
        columnDefs: [
          { name:'name', field: 'name' },
          { name:'isActive', field: 'isActive'},
          { name:'template',cellTemplate:'<div><a ng-show="hi">Active</a><a ng-hide={{row.entity.isActive=="N"}}>Deactive</a></div>'}
        ]
  };

  $scope.waiting = "Waiting...";
  $timeout(function () {
    $scope.gridOpts.data = [{ name: 'Bob' ,isActive:'Y'}];
  }, 3000)
  .then(function() {
    $timeout( function() {
      $scope.gridOpts.data = [{ name: 'John',isActive:'N' }];
    }, 3000);
    $scope.waiting = "Waiting again...";
  })
});

http://plnkr.co/edit/eLFjp8qJCtBRLTrQ7GE5?p=preview

ui-grid 使用隔离范围创建其网格,这意味着该模板背后的范围不是您在其中定义模板的范围。

但是,该独立作用域包含对此原始作用域的引用,您可以通过 grid.appScope.

访问该作用域

如果将 hi 更改为 grid.appScope.hi,则您的示例有效。 Forked version here.

查看 ui-grid API docs on appScope:

appScope: reference to the application scope (the parent scope of the ui-grid element). Assigned in ui-grid controller

use gridOptions.appScopeProvider to override the default assignment of $scope.$parent with any reference