从angular .js中传入ng-grid变量

pass variable from ng-grid Angulars.js

本文关键字:ng-grid 变量 angular js      更新时间:2023-09-26

我试图在另一个控制器中使用ng-grid表中的变量,

我的ng-grid的一部分定义如下:

  app.controller('ng-grid-controller'){
        ....
    $scope.gridOptions = {
        data:MyData,
        columnDefs: [
                    {field:'_id'},{name:'name',cellTemplate: 'templates/show.html'}]
    };
    $scope.getDetail = function(_id){
              alert(_id);
              var test=_id;
              console.log(test);
              return test;
    };
 } 

我能够在我的模板show.html中显示如下所示的值:

<div ng-style="{'cursor': row.cursor}" ng-click="getDetail(row.getProperty('_id'));" data >
</div>

但是我的问题是我如何在另一个控制器中访问这个值,我需要传递这个值作为另一个模板的参数:

在我的路由中,我有这样的内容:

  when('/show/:thatValue', {// Here I want that value I display=thatValue
    templateUrl: 'templates/displayvalue.html',
    controller: 'ShowController'
  }).

在我的控制器中:

   app.controller('ShowController', function($scope) {
        $scope.value='the value I display';//Something like this
    });

当你需要在控制器之间共享数据时,最好使用service,然后你可以将该值注入到各自的控制器中。例如:

app.factory('SomeService', [ '$log', function($log) {
   return {
      variable: null,
      getVariable: function() {
         return this.variable;
      },
      setVariable: function(value) {
         this.variable = value;
      }
   };
}]);

然后在你的控制器中使用:

app.controller("MyController", [ '$scope', 'SomeService', function($scope, SomeService) {
    $scope.someMethod = function() {
       $scope.variable = SomeService.getVariable();
    };
    $scope.setSomeService = function(value) {
       SomeService.setVariable( value );
    }
}];