2017-10-19 99 views
0

如果您不了解我的方法,请让我知道。它基于Angular-UI-grid。我有两个控制器(ctrl1.js,ctrl2.js)。我从不同的web服务的每个控制器上使用get方法获取数据。在Angular-UI-grid中enableCellEditing

timeSeries是我在Ctrl1.js中调用的webservice(getTimeSeriesData)中的对象。 timeSeries中有1500个对象,最后一个对象(第1500个)的最新对象为true,而其他对象的最新对象为undefined。如果最新版本为true,我应该可以编辑(在ctrl2.js中),否则用户不应该有任何功能来编辑列。

Ctrl1.js代码

$rootScope.test = false;  
     $rootScope.latest = timeSeries.latest; 
     if ($rootScope.latest == true){ 
      $rootScope.test = true; 
      console.log($rootScope.test); 
     } // everything is good here, I am getting true in console here. 

为了让我使用Ctrl2.js :-(问题两种方式的单元格编辑在这里它的不可编辑即cellEdit是假的。)

cellEditableCondition: function($rootScope){ 
         return $rootScope.test 
         }, 

enableCellEdit: $rootScope.test; 

回答

0
Added an extra button click to display the value. 

    <div> 
     <h1>Testing</h1> 
     <div ng-controller="secondCtrl as second"> 

     <button type="submit" ng-click='second.getStatus()'>Add Message</button> 
     <p> {{second.enableCellEdit}}</p> 
     </div> 

     <div ng-controller="firstCtrl as first"> 

     </div> 
    </div> 



    var app = angular.module('myapp', []); 


    app.controller('firstCtrl', function ($rootScope){ 

     $rootScope.test = false;  
     $rootScope.latest = true; 
     if ($rootScope.latest === true){ 
      $rootScope.test = true; 
      console.log($rootScope.test); 
     } 

    }); 

    app.controller('secondCtrl', function ($rootScope){ 
     var self = this; 

     self.getStatus =function(){ 
      self.enableCellEdit = $rootScope.test; 
     } 


    }); 

如果要在两个不同的模板上共享两个不同控件之间的数据,使用Factory是更好的方法。工厂数据共享请参考POST

+0

我无法使用rootscope从controller1中获取controller2中的值 – sahil

+0

很令人困惑的语句。根据描述,我理解你想访问你在控制器2(第二Ctrl)中的控制器1(第一个Ctrl)中设置的数据。如果相同的上述解决方案工作。在按钮上单击运行解决方案后,您可以在控制器2中设置真正的值,因为它已在控制器1中设置。请您详细说明您正在寻找的是什么。 –