0

I have the following code...

var app = angular.module('plunker', []);
TestController = function ($scope, $interval) {
  $scope.test = this;
  $scope.name = 'World';
    this.init();
    $interval(function () {
        //Added this because we shouldn't update until we either get the user data or that request fails
        $scope.test.init();
    }, 500);
};
TestController.prototype.init = function () {
    console.log("Test this thing");
};
app.controller('MainCtrl', TestController);

This works great but now I need to include the init function in another controller so I want both to inherit from a common prototype. However, when I try this plunker it doesn't seem to work.

What is the proper way to handle this sort of thing in JS?

7
  • in plunker you have function TimeBox but in prototype try assign TestController.prototype = new Test() Commented Sep 22, 2014 at 18:42
  • Thanks updated still has the same issue Commented Sep 22, 2014 at 19:08
  • 1
    all work if you define all function in app.js file, or add reference to test.js in index.html Commented Sep 22, 2014 at 19:53
  • 1
    if you want use code from test.js in page index.html - you need add reference to this script file like <script src="test.js"></script> :-) Commented Sep 23, 2014 at 5:49
  • 1
    Just include test.js before app.js ;) Commented Sep 24, 2014 at 5:42

1 Answer 1

2

It is TypeScript example http://plnkr.co/edit/HOeJ7nkkp2qRl53zEvVj?p=preview

TypeScript:

class Controller {
  static $inject = ['$scope']; // deps

  constructor($scope) {
    $scope.vm = this;
  }

  value(): string {
    return '123';
  }
}

class NewController extends Controller {
  constructor($scope) {
    super($scope);
  }

  value(): string {
    return super.value() + '456';
  }
}

declare var angular: any;
angular.module('my', []).controller('controller', NewController);

In JavaScript it looks like:

//Compiled TypeScript

var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
}

var Controller = (function () {
    function Controller($scope) {
        $scope.vm = this;
    }
    Controller.$inject = [
        '$scope'
    ];
    Controller.prototype.value = function () {
        return '123';
    };
    return Controller;
})();

var NewController = (function (_super) {
    __extends(NewController, _super);
    function NewController($scope) {
        _super.call(this, $scope);
    }
    NewController.prototype.value = function () {
        return _super.prototype.value.call(this) + '456';
    };
    return NewController;
})(Controller);

angular.module('my', []).controller('controller', NewController);

If you need OOP in JavaScript use smth like http://prototypejs.org/

Sign up to request clarification or add additional context in comments.

1 Comment

I will take a look at what you have posted and check after I get a chance to digest thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.