Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Created February 14, 2014 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcdnl/9007665 to your computer and use it in GitHub Desktop.
Save amcdnl/9007665 to your computer and use it in GitHub Desktop.

Inheritance in AngularJS.

angular.module('animal', [])
    .factory('Animal',function(){
        return function(vocalization){
            return {
                vocalization:vocalization,
                vocalize : function () {
                    console.log('vocalize: ' + this.vocalization);
                }
            }
        }
    });    
    
    angular.module('app', ['animal'])
        .factory('Dog', function (Animal) {
            function ngDog(){
                this.prop = 'my prop 1';
                this.myMethod = function(){
                    console.log('test 1');
                }
            }
            return angular.extend(Animal('bark bark!'), new ngDog());
        })
        .factory('Cat', function (Animal) {
            function ngCat(){
                this.prop = 'my prop 2';
                this.myMethod = function(){
                    console.log('test 2');
                }
            }
            return angular.extend(Animal('meooow'), new ngCat());
        })
    .controller('MainCtrl',function($scope,Cat,Dog){
         $scope.cat = Cat;
         $scope.dog = Dog;
         console.log($scope.cat);
         console.log($scope.dog);
        //$scope.cat = Cat;
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment