Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?); // 
share|improve this question
    
Why are you instantiating the controller yourself? Angular is responsible for creating the controllers and injecting the dependencies. –  gregpakes Aug 14 '13 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? –  zoidbergi Aug 14 '13 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. –  gregpakes Aug 14 '13 at 8:45
    
how can I call a method of the controller from outside then? –  zoidbergi Aug 14 '13 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. –  gregpakes Aug 14 '13 at 8:52
show 1 more comment

2 Answers

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)

share|improve this answer
    
I opened a new question: stackoverflow.com/questions/18228723/typescript-access-function –  zoidbergi Aug 14 '13 at 10:04
add comment

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;
        }

        ...
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.