0

I am using Typescript with Angular as described here http://www.youtube.com/watch?v=WdtVn_8K17E

class MyController {
    thescope: any;
    static $inject = ['$scope'];

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

    ...
}

I want to create an instance of this controller. What do I have to use as the $scope parameter?

var mc = new MyController(whatParameterHere?); // 
6
  • Why are you instantiating the controller yourself? Angular is responsible for creating the controllers and injecting the dependencies. Commented Aug 14, 2013 at 8:28
  • I use a javascript component (jstree) where i have a nodeselected event where I want to call a function of the controller. Maybe I have wrong design? Commented Aug 14, 2013 at 8:30
  • I think you probably do. I am not an expert, but I've never instantiated my own controller. Angular does that for you and manages the dependency injection at the same time. Commented Aug 14, 2013 at 8:45
  • how can I call a method of the controller from outside then? Commented Aug 14, 2013 at 8:46
  • In the past, I have set JQuery up as a dependency in angular and passed that into the controller. Then I create an init function on the controller and bind it to ng-init. I then create the jQuery component (jstree) in the init function. If the jsTree has an event then you should be able to wire up that event inside the controller and call whatever controller based function you wish. Without code, it is hard to give you a proper solution. Commented Aug 14, 2013 at 8:52

5 Answers 5

1

If your looking for a common method to call between controllers and or directives, use a service.

If your looking to manipulate the dom, use a directive.

To answer your question:

module myApp.ctrl {
    myApp.controller("MyController", function($scope) {
        return new MyController($scope);
    }

    class MyController {
        thescope: any;
        static $inject = ['$scope'];

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

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

Comments

1

you should use a service not controller,

service you can inject it and call its function in your app.

for example, if you want to call a data from an api:

module Data {
    export interface IDataService {
        http:ng.IHttpService;
        fetch:()=>ng.IPromise<string>;
    }
    export class DataService implements IDataService{
        http:ng.IHttpService;

        static $inject = ["$http"];
        constructor($http:ng.IHttpService) {
            this.http = $http;
        }

        fetch():ng.IPromise<string> {
            return this.http.get("link");
        }
    }
}
angular.module("app").service("DataService", Data.DataService);

if you want to use a plugin like jstree, you should create a directive for that and inject DataService in it and use the function u want.

Comments

0

In AngularJS your dom manipulation should go in a directive. In your case you would create a Directive for JSTree. This answer should give you a head start : How to use jsTree events with AngularJS

To learn more about directives here is a nice video from the creator of AngularJS : https://www.youtube.com/watch?v=WqmeI5fZcho (I will make an AngularJS + TypeScript Directives video at some point)

1 Comment

0

As others have suggested, you should not do this yourself, angular will do it yourself.

But it can be done by just passing in $scope as a string, angular will do DI for you, so your code should look like this

var mc = new MyController("$scope"); 

Comments

0

typically you would use a private variable declared as a parameter to hold the injected $scope object, like so:

..

class MyController {
  
  static $inject = ['$scope'];
  constructor(private scope: ng.IScope){
    this.init();  
  }
  
  private init() {
    this.scope.foo = 'bar';
  }
  
}

..

Though the $inject statement isn't essential it states explicitly that $scope is an injected asset :-)

You would then create an instance of MyController in the angular way, as follows:

angular.module('app')
  .controller('MyController', MyController);

Comments

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.