$onInit not being called inside directive controller

Tobias Persson_
Tera Expert

Hi,

 

I am currently creating a new Angular.js directive for my company. I like to use the $onInit function to initialize code. This usually works fine. But when I want $onInit to initialize my Angular Directive it just doesn't fire at all.

 

See this snippet here:

function myDirective() {
  return {
    controller: function($scope) {
      var c = this;
      c.$onInit = function() {
        console.log('myDirective $onInit fired...'); // <-- this never happens
      }
    },
    restrict: 'E',
    scope: {
      myVar: '='
    }
    template: '<div>Some HTML templating...</div>'
  };
}

 

The code above should work fine - except that c.$onInit has absolutely no effect. $onInit does not seem to be called inside the directive.

 

Is there a known issue inside ServiceNow Angular.js directives? I found some reports that Angular.js has this behaviour when a controller gets initialized by ngRoute. Is this maybe happening in the background?

 

Thanks and regards,

Tobias

1 ACCEPTED SOLUTION

Tobias Persson_
Tera Expert

I have no idea what changed... searched 1 hour to find out why it is not working. And no it suddenly just triggered the $onInit without any issues. And as I said... no idea what did the trick.

View solution in original post

1 REPLY 1

Tobias Persson_
Tera Expert

I have no idea what changed... searched 1 hour to find out why it is not working. And no it suddenly just triggered the $onInit without any issues. And as I said... no idea what did the trick.