AngularJS:如何从ui-grid cell模板访问范围?

ten*_*our 41 javascript angularjs angular-ui-grid

如何$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>'
    }]
  };
}]);
Run Code Online (Sandbox Code Playgroud)

请在此处查看:http: //plnkr.co/edit/WYXeQShHWKDYDs4MIZnP?p = preview

我希望细胞内容显示"你好世界",但他们只是显示"你好".

ten*_*our 69

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

  cellTemplate: '<div>{{ "hello " + grid.appScope.world() }}</div>'
Run Code Online (Sandbox Code Playgroud)

  • 来自filterHeaderTemplate我不得不使用'col.grid.appScope.<ctrl alias> .myFunction()' (2认同)