AngularJS:如何从UI网格单元格模板访问范围?

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

 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

我希望细胞内容显示“你好世界”,但他们只是显示“你好”。

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

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