javascript AngularJS + NG-Grid 将 row.column.field 传递给 ng-click 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19822133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:55:35  来源:igfitidea点击:

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

javascriptangularjsangular-uing-grid

提问by Woot4Moo

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.

一个怎么{{row.getProperty(col.field)}}进入ng-click?发生的情况是 id 没有被传播回来,但网格使用 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>'  
              }]
      };    
});  

回答by AardVark71

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

您可以只在 ng-click 中传递 row,而不仅仅是双括号表达式输出的值。

Your cellTemplate becomes this

你的 cellTemplate 变成了这个

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;
};