Is there a way to call $onInit function again in another method?

68 views Asked by At

The problem I am using two different grids both are calling the same controller and grid Component. And I want to show/unshow a checkbox in one gird based on a Condition. Since the gridOptions are loaded while initiating OnInit Method I am not able to change the enableRowSelection method on click of another grid.

I tried the code shown below but the registerAction did not get called though I call the callOnInitFunction again.

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

I even tried to put the $onInit Function in separate Method like below and call the method again but got compilation error that time.

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

Can anyone please guide me on this.Thanks in Advance :)

1

There are 1 answers

3
MJTech On

You could just do:

this.$onInit = this.callOnInitFunction;

Or, if the function become inaccessible after the assignment, you can try defining the function globally...

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

...and assigning it to this.callOnInitFunction:

this.callOnInitFunction = start;

Then you can call start() in other places.