Search code examples
angularjstypescriptqdefinitelytyped

How to use Typescript Q.when()


I have a function that returns angular.IPromise<any>

onActivate():IPromise<any>
{
     return Q.when(true);
}

but when it runs this it says

Q is not defined

Although, when I look in Q.d.ts file, the module is declared and the function when is exported.

How to use Q service in typescript classes?

The entire class is this:

/// <reference path="../../typings/q/q.d.ts" />
module app.common.modals
{

export class RenameModalCtrl extends app.common.controllers.ControllerBase
{
    public viewModel: RenameModalModel;
    private $modalInstance: ng.ui.bootstrap.IModalServiceInstance;

    static $inject = ['common', '$translate', '$modalInstance', 'viewModel'];

    constructor(common: any, $translate: angular.translate.ITranslateService, $modalInstance: ng.ui.bootstrap.IModalServiceInstance, viewModel: RenameModalModel)
    {
        super(common, $translate);
        this.viewModel = viewModel;
        this.$modalInstance = $modalInstance;
    }

    onActivate(): ng.IPromise<any>
    {
        return Q.when(true);
    }       
}

angular.module('app').controller('renameModalCtrl', RenameModalCtrl);
}

Thanks


Solution

  • You have to add $q as one of your injectables in $inject. Then add it to your constructor as type Q: ng.IQService

    edit: Thought you meant the angular q. I think you just need to import the module:

    import * as Q from 'q';