javascript - Angular : How to access scope from ui-grid cell template?

标签 javascript angularjs angular-ui-grid

如何从 ui-grid 单元格模板访问 $scope?这是我的 Controller 代码:

app.controller('MainCtrl', ['$scope', function ($scope) {

  // i want to reference this from a cell template.
  $scope.world = function() { return 'world'; };

  $scope.gridOptions = {
    data: [
      { id: "item1" },
      { id: "item2" }
    ],
    columnDefs: [
    {
      field: 'id',

      // world() is never called and is not displayed.
      cellTemplate: '<div>{{ "hello " + world() }}</div>'
    }]
  };
}]);

在此处查看实际效果: http://plnkr.co/edit/WYXeQShHWKDYDs4MIZnP?p=preview

我希望单元格内容显示“hello world”,但它们只显示“hello”。

最佳答案

根据 http://ui-grid.info/docs/#/tutorial/305_appScope , 网格有它自己的独立作用域,所以你需要使用 grid.appScope 来访问你的应用程序作用域。解决方案是将单元格模板更改为:

  cellTemplate: '<div>{{ "hello " + grid.appScope.world() }}</div>'

关于javascript - Angular : How to access scope from ui-grid cell template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28127207/

相关文章:

angularjs - 如何将序列号添加到 Angular ui 网格中的行

javascript - 更改kendo grid数据源使用JS

javascript - 仅将响应式菜单重写为 css

javascript - Angular 模拟版本与 Angular 版本的关系

javascript - 永远不要同时显示两个 div

javascript - 导出到 ui-grid 中的 csv : exporterAllDataFn fires only when we have more than one page

javascript - 如何使 slider 移动到页面的 100%

javascript - addEventListener ('exit' ) 到 PhoneGap 中的 inappbrowser

javascript - 为什么在使用 ng-options 时我不能在 select 中有多个 ng-if 选项

javascript - 在 UI 网格链接中调用范围函数