Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extend controller component - angularjs

I use angular 1.5 to develop my application and I am using .component(). I have three components and their controllers, all of which are quite similar. How can I extend the controller from comp1 to use it with comp2?

Each component in a separate js file:

comp1.js comp2.js comp3.js

like image 518
Amine Avatar asked Oct 30 '22 01:10

Amine


2 Answers

You can extend component controllers from each other as well. Use the following approach:

Parent component (to extend from):

/**
 * Module definition and dependencies
 */
angular.module('App.Parent', [])

/**
 * Component
 */
.component('parent', {
  templateUrl: 'parent.html',
  controller: 'ParentCtrl',
})

/**
 * Controller
 */
.controller('ParentCtrl', function($parentDep) {

  //Get controller
  const $ctrl = this;

  /**
   * On init
   */
  this.$onInit = function() {

    //Do stuff
    this.something = true;
  };
});

Child component (the one extending):

/**
 * Module definition and dependencies
 */
angular.module('App.Child', [])

/**
 * Component
 */
.component('child', {
  templateUrl: 'child.html',
  controller: 'ChildCtrl',
})

/**
 * Controller
 */
.controller('ChildCtrl', function($controller, $parentDep) {

  //Get controllers
  const $ctrl = this;
  const $base = $controller('ParentCtrl', {$parentDep});

  //Extend
  angular.extend($ctrl, $base);

  /**
   * On init
   */
  this.$onInit = function() {

    //Call parent init
    $base.$onInit.call(this);

    //Do other stuff
    this.somethingElse = true;
  };
});

You can define new method in the child controller, overwrite existing methods, call the parent methods, etc. Works really well.

like image 160
Adam Reis Avatar answered Nov 12 '22 12:11

Adam Reis


I may suggest that you simply use services to share and compose components. You can then skip the complexities of worrying about .extend(), $controller, etc.

  angular
    .module('app')
    .factory('component.utils', function() {
       return {
         sharedProp: 'foo',
         sharedMethod: function() { return 'bar' }
       }
    })
    // components can all easily use functionality 
    // provided by one (or more) services, w/o 
    // needing a complicated inheritance model.
    .component('foo', {
      templateUrl: 'foo.html',
      controller: [
        'component.utils',
        function(utils) {
          this.$onInit = function() {
            this.prop = utils.sharedProp;
            utils.sharedMethod();
            // now do other foo things...
          }
        }
      ]
    })
    .component('bar', {
      templateUrl: 'foo.html',
      controller: [
        'component.utils',
        function(utils) {
          this.$onInit = function() {
            this.prop = utils.sharedProp;
            utils.sharedMethod();
            // now do other bar things...
          }
        }
      ]
    });

Inheritance has its place, but favoring composition over inheritance is usually the better solution. article.

like image 26
benjaminapetersen Avatar answered Nov 12 '22 12:11

benjaminapetersen