4

I already added the AngularJS definitions. Presently my app controller looks like this:

app.controller('appController',
    [
        '$scope',
        '$state',
        'utilityService',
        appController
    ]);

function appController(
    $scope: ng.IScope,
    $state,
    utilityService
    ) {

    var app = this;

    $scope.broadcast = function (val) {
        $scope.$broadcast('broadcast', val);
    };

    if (app.auth.user.isAuthenticated()) {
        app.state.text = null;
        $state.transitionTo('home.content', { content: 'overview' });
    } else {
        $state.transitionTo('authentication');
        // app.auth.loginData = app.cache.get('loginData');
        app.state.text = "Please Login ...";
    }

} 

Should I convert into a class if I want to take full advantage of TS and if so how do I handle the definition of functions like the broadcast function?

2 Answers 2

5

Here is the principle

app.controller('appController', AppController);

interface AppControllerScope extends ng.IScope{
    vm: AppController; 
}

class AppController{

    text: string;

    static $inject=[
        '$scope',
        '$state',
        'utilityService'
    ]
    constructor(
    public $scope: AppControllerScope,
    public $state,
    public utilityService: UtilityService
    ) {
        $scope.vm = this; 

        // NOTE: unclear what app is, perhaps its a service?
        if (app.auth.user.isAuthenticated()) {
            this.text = null;
            $state.transitionTo('home.content', { content: 'overview' });
        } else {
            $state.transitionTo('authentication');
            // app.auth.loginData = app.cache.get('loginData');
            this.text = "Please Login ...";
        }
    }

    broadcast = (val) => {
        this.$scope.$broadcast('broadcast', val);
    };
} 

Gist:

  • You access the controller functions form the view using vm.something e.g. ng-click="vm.broadcast('tada')"
  • Also use classes for services e.g. utilityService is an instance of UtilityService and you get typesafety. More: https://www.youtube.com/watch?v=Yis8m3BdnEM&hd=1

More the use class as controller pattern : https://www.youtube.com/watch?v=WdtVn_8K17E&hd=1

2
  • thanks. I have a question. When I enter $scope.vm = this it gives me an error saying: the property vm does not exist on type ng.IScope. Can you advise me on this?
    – user3568783
    Commented Jul 26, 2014 at 11:22
  • 1
    Thanks a lot. I am going to work on some more classes and might have more questions later. Thanks for your help.
    – user3568783
    Commented Jul 26, 2014 at 11:43
0

here is how i do

module protal{

   var app =angular.module('portal',[]);
   app.controller(controllers)          
}


module portal.controllers{


 // register any no of controllers here in this namespace
export class masterController{
    public fooFunc(){

    }
   static $inject=['$scope'];
    constructor($scope){
        $scope.vm=this;
    }

  }
}

Your Answer

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