Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an Angular service using typescript?

I have a service code using typescript and AngularJS like this :

/// <reference path='../_all.ts' />

module bankApp {
    'use strict';

    export class MDCurrencyService implements IMDCurrencyService {
        httpService: ng.IHttpService;
        promise: ng.IPromise<void>;

        constructor($http: ng.IHttpService,
            $q : ng.IQService) {

            this.httpService = $http;
        }

        get(): MDCurrency[] {
            var promise = this.httpService.get('/Master/CurrencyGetAll').then(function (res) {
                return res.data;
            });
            return promise;
        }


        save(cur: MDCurrency) {
            this.httpService.post('/Master/CurrencySave', cur);

        }

        softDelete(id: string)
        { }

        hardDelete(id: string)
        { }




    }
}

I will use my controller like this :

this.currencies = $scope.currencies = mdCurrencyService.get();

How do I make an angular service $http using typescript? I'd like it so that this.currencies in my controller will be filled with data from the server.

like image 873
yozawiratama Avatar asked Jun 06 '15 15:06

yozawiratama


People also ask

How do you create a service in TypeScript?

Test Service Worker Locally First make sure to compile the TypeScript service worker code by using the command npm run build-typecheck . Then, to install the http-server npm package run the command npm i http-server --save-dev . After installing run the command http-server in your project folder where the index.

What is service TS in Angular?

Services in Angular are simply typescript classes with the @injectible decorator. This decorator tells angular that the class is a service and can be injected into components that need that service. They can also inject other services as dependencies.

How do I create a service in Angular 8?

Create Angular serviceAn Angular service is plain Typescript class having one or more methods (functionality) along with @Injectable decorator. It enables the normal Typescript class to be used as service in Angular application. Here, @Injectable decorator converts a plain Typescript class into Angular service.


Video Answer


1 Answers

The service should look like the following. Don't forget registering the service in the module:

export class MDCurrencyService implements IMDCurrencyService {
    constructor(private $http: ng.IHttpService, private $q : ng.IQService) {
    }

    get(): ng.IPromise<MDCurrency[]> {
        var deferred = this.$q.defer();
        this.$httpService.get('/Master/CurrencyGetAll').then(response => {
            deferred.resolve(response.data);
        }).catch( reason => {
            deferred.reject(reason);
        });
        return deferred.promise;
    }
}

angular.module("TheModule").service("mdCurrencyService", MDCurrencyService);

The controller should look like this:

mdCurrencyService.get().then(currencies => {
   this.$scope = currencies;
}).catch( reason => {
   alert("something went wrong!");
});

EDIT:

The code can be simplified, the $q service is not required:

export class MDCurrencyService implements IMDCurrencyService {
    constructor(private $http: ng.IHttpService) {
    }

    get(): ng.IPromise<MDCurrency[]> {          
        return this.$httpService.get('/Master/CurrencyGetAll')
                   .then(response => response.data);
    }
}

angular.module("TheModule").service("mdCurrencyService", MDCurrencyService);
like image 70
Jesús López Avatar answered Nov 15 '22 04:11

Jesús López