Javascript AngularJS:如何从 ui-grid 单元格模板访问范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28127207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:15:40  来源:igfitidea点击:

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

javascriptangularjsangular-ui-grid

提问by tenfour

How does one access $scopefrom a ui-grid cell template? Here's my controller code:

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

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>'
    }]
  };
}]);

See it in action here: http://plnkr.co/edit/WYXeQShHWKDYDs4MIZnP?p=preview

在这里查看它的实际效果:http: //plnkr.co/edit/WYXeQShHWKDYDs4MIZnP?p=preview

I would expect cell contents to show "hello world", but they just show "hello".

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

回答by tenfour

According to http://ui-grid.info/docs/#/tutorial/305_appScope, the grid has its own isaloted scope, so you need to use grid.appScopeto access your application scope. The solution is to change the cell template to:

根据http://ui-grid.info/docs/#/tutorial/305_appScope,网格有自己的 isaloted 范围,因此您需要使用grid.appScope来访问您的应用程序范围。解决方案是将单元格模板更改为:

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