0

Here is my service.

export module cropyear_services {
//export interface ICropYearService {
//    getFieldYearData(): any;

//}
export class CropYearService {    

    static $inject: string[] = ["$q", "$filter", "$httpBackend", "$http"];
    constructor(private _q: ng.IQService, private _http: ng.IHttpService, private _filter: ng.IFilterService, private _$httpMock: ng.IHttpBackendService) { }

    getFieldYearData = function () {

        let deferred = this._q.defer();
        let uri = endpoints.Endpoints.endpoints.fieldyears;
        this._http.get(uri).then((fieldYearResultData: any) => {
            let fieldYearData = fieldYearResultData.data[0].ResultSet;
            deferred.resolve({ result: fieldYearData });
        }).catch(() => {
            deferred.reject();
        });         

        return deferred.promise;
    }

 }
}

And Here is my controller where I am calling the service method.

/// <reference path="../../../../thirdparty/lodash/lodash.d.ts"/>
/// <reference path="../../../../thirdparty/angular-material/angular-material.d.ts"/>
 /// <reference path="../../../../thirdparty/angular/angular.d.ts"/>
/// <reference path="../../../../thirdparty/angular-ui-router/api/angular-ui-router.d.ts"/>
/// reference path="./cropyear.service.ts"/>
/// reference path= "../../../../common/sharedData/TSModule/sharedData.ts'/>
import service = require('./cropyear.service');
import SharedDataFactory =    require('../../common/sharedData/TSModule/sharedData'); 

export module cropyear_controllers {
export class CropYearController {
    static $inject: string[] = ["$log", "$scope", "cropYearService"];
    constructor(/*private _log: ng.ILogService,*/ public _scope: ng.IScope,   public _cropYearService:  service.cropyear_services.CropYearService
        , public _sharedData: any, public fieldYears: any) {
        this._sharedData = SharedDataFactory.sharedData_factories.SharedData;
    }
    loadYears() {            
        this._cropYearService.getFieldYearData()
            .then((data: any) => {
                this.fieldYears = _.uniq(_.pluck(data.result, 'CropYear'));
            })
            .catch(() => {
                console.log('Error calling farm view  service');
            });
    }
    hangeCropYear(year: number) {
        this._sharedData.selectCropYear(year);
    }
}
}

Vs compiles it with no errors. When I try to rise the loadYears(), i get error in console that Uncaught TypeError: this._cropYearService.getFieldYearData is not a function. Can any one point out where is my mistake?

2
  • getFieldYearData = function (), should be getFieldYearData: function () Commented Oct 5, 2015 at 22:12
  • I did try that, compiler throws need type error. Commented Oct 5, 2015 at 22:21

2 Answers 2

2

Figured it. That's because of the order of $inject.

Sign up to request clarification or add additional context in comments.

Comments

1

When I try to rise the loadYears(), i get error in console that Uncaught TypeError: this._cropYearService.getFieldYearData is not a function

The meaning of this in loadYears is driven by the caller. Since you haven't shared how you are calling loadYears I suspect that the fix is a simple change to arrow function:

loadYears = () => {            
    this._cropYearService.getFieldYearData()
        .then((data: any) => {
            this.fieldYears = _.uniq(_.pluck(data.result, 'CropYear'));
        })
        .catch(() => {
            console.log('Error calling farm view  service');
        });
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.