最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS + NG-Grid pass row.column.field into ng-click event - Stack Overflow

programmeradmin6浏览0评论

How does one pass {{row.getProperty(col.field)}} into ng-click? What happens is the id does not get propagated back, but the grid render correctly with the id.

code:

var app = angular.module('testing',['ngGrid']);
app.config(['$locationProvider', function($locationProvider)  
{  
       $locationProvider.html5Mode(true);
}]);    

app.controller('TestCtrl',function($scope)  
{  
      $scope.details = []; //whatever dummy data  
      $scope.loadById = function(id)  
      {  
            $window.location.href= 'newPage/?id='+id;
      };  

      $scope.gridOptions =  
      {  
           data: 'details',  
           columnDefs:[{field:'id',DisplayName:'id',  
                 cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><a ng-click="loadById({{row.getProperty(col.field)}})">{{row.getProperty(col.field)}}</a></div>'  
              }]
      };    
});  

How does one pass {{row.getProperty(col.field)}} into ng-click? What happens is the id does not get propagated back, but the grid render correctly with the id.

code:

var app = angular.module('testing',['ngGrid']);
app.config(['$locationProvider', function($locationProvider)  
{  
       $locationProvider.html5Mode(true);
}]);    

app.controller('TestCtrl',function($scope)  
{  
      $scope.details = []; //whatever dummy data  
      $scope.loadById = function(id)  
      {  
            $window.location.href= 'newPage/?id='+id;
      };  

      $scope.gridOptions =  
      {  
           data: 'details',  
           columnDefs:[{field:'id',DisplayName:'id',  
                 cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><a ng-click="loadById({{row.getProperty(col.field)}})">{{row.getProperty(col.field)}}</a></div>'  
              }]
      };    
});  
Share Improve this question asked Nov 6, 2013 at 20:27 Woot4MooWoot4Moo 24.3k18 gold badges98 silver badges156 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 14

You can just pass row in ng-click, instead of only the value outputted by the double brackets expression.

Your cellTemplate becomes this

cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><a ng-click="loadById(row)">{{row.getProperty(col.field)}}</a></div>' 

Your function becomes this

$scope.loadById = function(row) {  
   window.console && console.log(row.entity);
   $window.location.href= 'newPage/?id='+ row.entity.id;
};  
发布评论

评论列表(0)

  1. 暂无评论