title: Why to use $onInit in AngularJS?
body: <p>In AngularJS, what is the purpose of the <code>$onInit</code> function if I can do the same initialization without that function?</p>
  
  <p>For example this:
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>module.component("myComponent", {
          ...
          controller: function() {
              const $ctrl = this;
  
              $ctrl.$onInit = () =&gt; {
                  $ctrl.var1 = "hello";
                  $ctrl.var2 = { test: 3 };
              }
          }
      });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Can also be done like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>module.component("myComponent", {
          ...
          controller: function() {
              const $ctrl = this;
  
              $ctrl.var1 = "hello";
              $ctrl.var2 = { test: 3 };
          }
      });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Is there some case where <code>$onInit</code> is necessary?</p>