0

I'm trying to access an API with angularResource.get(params)

The model for the class looks like this:

export class d_stp_searchbar implements Id_stp_searchbar {

    lperiode?: number;
    private _lsichtweise: number;
    scode?: string;
    sbezeichnung?: string;
    bezeichnungLabel: string;

    set lsichtweise(value) {
        this._lsichtweise = value;
        this.bezeichnungLabel = this.lsichtweise === 5 ? "Name" : "Bezeichnung";
    }

    get lsichtweise() {
        return this._lsichtweise;
    }

    public bezeichnung() {
        if (this.lsichtweise === 5) {
            this.bezeichnungLabel = "Name";
        } else {
            this.bezeichnungLabel = "Bezeichnung";
        }
    }
}

The function, where i call the api looks like the following:

searchStundenplan(params: d_stp_searchbar): angular.IPromise<any> {
    this.searchStundenplanParams = params;
    this.localStorageService.set("searchStundenplanParams", this.searchStundenplanParams);

    return this.searchStundenplanResource.get(params)
        .$promise.then((result) => {
            this.searchStundenplanResult = result;
            this.searchStundenplanResultChangedNotify();
        });
 }

The problem I'am facing is, that the URL that will be called looks like this:

GET /api/search/Stundenplan?_lsichtweise=1&bezeichnungLabel=Bezeichnung&lperiode=86

but I want, that instead of _lsichtweise lsichtweise will be used.

Do I use it wrong or does somebody know, why not the public property lsichtweise is used?

Thanks in advance

4
  • 1
    There is no public property lsichtweise in your class d_stp_searchbar. You created a magic function get but angular.IPromise does only recognize class vars and ignores the magic getter function. Just add a public class property lsichtweise: number;. Maybe angular.IPromise will call the magic getter function and you will be fine. Commented Feb 1, 2017 at 9:36
  • An other attemp is to remap your model attributes while they are parsed as params into angular.IPromise. Which solution you would prefer? Commented Feb 1, 2017 at 10:18
  • The problem was, that the d_stp_searchbar was only instantiated properly the first time we called it. So your comment was not the solution, but a gamechanger for further investigations. Thank you! Commented Feb 1, 2017 at 12:48
  • Glad to help. Please post your solution for other users. Commented Feb 1, 2017 at 12:50

1 Answer 1

1

I was able to fix it by myself:

The problem was, that the d_stp_searchbar was only instantiated properly the first time we called it.

We were loading a JSON into a TypeScript object like this:

this.searchParamsStundenplan = localStorageService.get("searchParamsStundenplan") as d_stp_searchbar;

but then the searchParamsStundenplan does not contain the (in the question mentioned) lsichtweise getter and setter because it was a plain data object without any functions.

So we had to create an instance the following way:

let searchParamsStundenplanTemp = new d_stp_searchbar();
searchParamsStundenplanTemp.bindinglsichtweise = this.searchParamsStundenplan.bindinglsichtweise;
searchParamsStundenplanTemp.lperiode = this.searchParamsStundenplan.lperiode;
searchParamsStundenplanTemp.scode = this.searchParamsStundenplan.scode;
this.searchParamsStundenplan = searchParamsStundenplanTemp;

This way it worked.

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

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.