AngularJs - $onInit - directive

by Julien Roche

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<my-directive my-label="a label"></my-directive>

JavaScript 1.7

angular
	.module('myApp', [])
  .directive('myDirective', function () {
  	return {
    	'restrict': 'E',
      'template': 'A simple directive with label "{{ vm.myLabel }}"',
      'scope': {
      	'myLabel': '@'
      },
      controller: function () {
      	this.$onInit = function () {
        	alert('$onInit method was called');
        };
        
        alert('Controller was initialized');
      },
      controllerAs: 'vm',
      bindToController: true,
    }
  });
  
  angular.bootstrap(document.body, ['myApp']);