如何从 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/