Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  37] [ 1]  / answers: 1 / hits: 38543  / 10 Years ago, sat, january 24, 2015, 12:00:00

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



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



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


More From » angularjs

 Answers
77

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



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

[#68099] Thursday, January 22, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leighrheac

Total Points: 313
Total Questions: 92
Total Answers: 94

Location: Papua New Guinea
Member since Thu, May 7, 2020
4 Years ago
;