3

I have a very simple controller:

.controller('ModalInstanceCtrl', function ($scope, $modalInstance, $userService) {

        $scope.ok = function () {
            $modalInstance.close();
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    })

How can I change this so that it uses typescript and so it will work even after I minify my javascript?

4
  • 3
    Valid JavaScript is valid TypeScript - congratulations, you did it. Commented Apr 16, 2015 at 6:27
  • @Marty True, and a good point. But there are some other advantages to making it use the native TypeScript class capabilities (and such) to clean it up and make it a little more maintainable at scale. I say that just in case that's your "unclear" VTC--it's good to know how to benefit from TypeScript, when you'll be using it anyway. Even if you don't have to. Commented Apr 16, 2015 at 6:43
  • @MatthewHaugen Of course - I was being a little cheeky. Commented Apr 16, 2015 at 6:44
  • @Marty Alright, just making sure. :) Commented Apr 16, 2015 at 6:46

1 Answer 1

9

Controllers and services can become classes.

I like to use $inject so it's safe to minify, but that line is optional.

class ModalInstanceController {
    static $inject = ["$scope", "$modalInstance", "$userService"];
    constructor($scope, $modalInstance, $userService) {

        $scope.ok = () => {
            $modalInstance.close();
        };

        $scope.cancel = () => {
            $modalInstance.dismiss('cancel');
        };
    }
}
.controller('ModalInstanceCtrl', ModalInstanceController);

Including $inject is equivalent to using the array syntax in vanilla JavaScript:

.controller('ModalInstanceCtrl', ["$scope", "$modalInstance", "$userService", function ($scope, $modalInstance, $userService) { ... }]);

In a real-world application, I like to avoid the use of $scope except for things that actually need it (like $watch), in which case I'd pull the methods out as well. This will require a change to your HTML, though.

class ModalInstanceController {
    private $modalInstance : any;

    static $inject = ["$modalInstance", "$userService"];
    constructor($modalInstance, $userService) {
        this.$modalInstance = $modalInstance;
    }

    ok() {
        this.$modalInstance.close();
    }
    cancel() {
        this.$modalInstance.dismiss('cancel');
    };
}

Then in your HTML,

<button type="button" ng-click="ModalInstanceCtrl.ok()">OK</button>

Note the use of the fully-qualified ModalInstanceCtrl.ok(), since it's no longer just floating around in the scope.


Just since you caught me when I was bored, here's a nice advantage of using TypeScript, since I see you have $userService.

class UserService {
    // A parameterized constructor is, of course, allowed here too.
    // Optionally supply $inject, per above

    parse(arg : string) {
        return parseInt(arg);
    }
}

class ModalInstanceController {
    private $modalInstance : any;
    private $userService : UserService; // Note the typing here

    static $inject = ["$modalInstance", "$userService"];
    // Explicit typing here is optional, since "any" will cast automatically
    // but I like to be clear anyway.
    constructor($modalInstance, $userService : UserService) {
        this.$modalInstance = $modalInstance;
        this.$userService = $userService;
    }

    ok() {
        // you'll get Intellisense here, whilst still benefiting from DI from Angular
        var arg = this.$userService.parse("12");

        this.$modalInstance.close();
    }
    cancel() {
        this.$modalInstance.dismiss('cancel');
    };
}
app.service("$userService", UserService);
app.controller("ModalInstanceCtrl", ModalInstanceController);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.