有没有办法在另一个方法中再次调用$onInit函数?

问题描述 投票:0回答:1

我使用两个不同网格的问题都调用相同的控制器和网格组件。我想根据条件在一个网格中显示/取消显示复选框。由于在启动 OnInit 方法时加载了 gridOptions,因此我无法在单击另一个网格时更改 enableRowSelection 方法。

我尝试了下面显示的代码,但尽管我再次调用了 callOnInitFunction,但 registerAction 没有被调用。

this.callOnInitFunction = function(){
              this.isSelected= function isSelected() 
                  {
                      if(flag)
                      {
                          return true;
                      }
                      else
                          return false;
                  };

                  this.gridOptions = {
                      enableRowSelection: this.isSelected(),
                      enableSelectAll: this.isSelected()
                      //other gridoption codes goes here the I am registering the grid
                    data: [],
                      registerAction: function ($scope, gridApi) {
                          gridService.registerGrid('gridName', gridApi);          
                      }
                }
         
   };
//then I am calling this function in onInit and again in the update Method
this.$onInit = function() {
                 this.callOnInitFunction();
};

我什至尝试将 $onInit 函数放入单独的方法中,如下所示,然后再次调用该方法,但当时出现了编译错误。

  this.callOnInitFunction = function(){
                this.$onInit = function();
             };

任何人都可以指导我吗?预先感谢:)

javascript angular angularjs grid ngoninit
1个回答
0
投票

你可以这样做:

this.$onInit = this.callOnInitFunction;

或者,如果函数在赋值后变得不可访问,您可以尝试全局定义该函数...

function start() { /* ... */ }

...并将其分配给

this.callOnInitFunction
:

this.callOnInitFunction = start;

然后你可以在其他地方拨打

start()

© www.soinside.com 2019 - 2024. All rights reserved.