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

38,058

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>'
Share:
38,058
tenfour
Author by

tenfour

https://twitter.com/tenfour2

Updated on September 26, 2020

Comments

  • tenfour
    tenfour almost 4 years

    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".

  • user2171669
    user2171669 over 8 years
    from filterHeaderTemplate I had to use 'col.grid.appScope.<ctrl alias>.myFunction()'
  • Admin
    Admin over 8 years
    What is <ctrl alias> exactly? In according to the example above?
  • FOR
    FOR over 8 years
    I can't be sure but the comment with <ctrl alias> seem to make sense for those using the "Controller as" syntax. So, if you've set up your controller to be available as an alias (commonly: controller as vm), then you need to include the alias in your chain, eg: grid.appScope.vm.myMethod(). HTH
  • Saurabh Tiwari
    Saurabh Tiwari over 7 years
    I tried this ng-change="grid.appScope.checkValidaton($event,MODEL_COL_FIE‌​LD,true,true). The scope function gets called however the arguments are undefined. How can I pass the $event and ng-model along