Skip to content

Instantly share code, notes, and snippets.

@andreasderuiter
Last active August 23, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasderuiter/12711103380d6e0a5c69 to your computer and use it in GitHub Desktop.
Save andreasderuiter/12711103380d6e0a5c69 to your computer and use it in GitHub Desktop.
module App {
"use strict";
interface IMyAppController {
values: string[];
errorMessage: string;
isVisibleErrorMessage: boolean
}
export class MyAppController implements IMyAppController {
errorMessage: string = "";
isVisibleErrorMessage: boolean = false;
values: string[] = [];
static $inject: string[] = ["$http", "$window"];
constructor(private $http: ng.IHttpService, private $window: ng.IWindowService) {
this.getValues();
}
private getValues(): void {
this.$http.get("/api/sample")
.then((response: ng.IHttpPromiseCallbackArg<string[]>) => {
this.isVisibleErrorMessage = false;
this.values = response.data;
})
.catch(((reason: ng.IHttpPromiseCallbackArg<string[]>) => {
this.isVisibleErrorMessage = true;
this.errorMessage = reason.statusText;
}
return this.values;
}));
}
}
angular.module("app").controller("MyAppController", MyAppController);
}
@crocodile
Copy link

This is a typescript file and not cs

@rachelppierson
Copy link

rachelppierson commented May 23, 2016

I think this should actually be:

module App {
    "use strict";

    interface IMyAppController {
        values: string[];
        errorMessage: string;
        isVisibleErrorMessage: boolean
    }

    export class MyAppController implements IMyAppController {
        errorMessage: string = "";
        isVisibleErrorMessage: boolean = false;
        values: string[] = [];

        static $inject: string[] = ["$http", "$window"];
        constructor(private $http: ng.IHttpService, private $window: ng.IWindowService) {
            this.getValues();
        }

        private getValues(): void {
            this.$http.get("/api/sample")
                .then((response: ng.IHttpPromiseCallbackArg<string[]>) => {
                    this.isVisibleErrorMessage = false;
                    this.values = response.data;
                })
                .catch(((reason: ng.IHttpPromiseCallbackArg<string[]>) => {
                    this.isVisibleErrorMessage = true;
                    this.errorMessage = reason.statusText;
                    //} <-- NB: Extra closing curly brace removed
                    return this.values;
                }));
        }
    }
    angular.module("app").controller("MyAppController", MyAppController);
}

Also, as crocodile says, it's a .TS file and not .CS

@yaboy58
Copy link

yaboy58 commented Jul 28, 2016

Thanks for the fix rachelppierson.

@abebatista
Copy link

abebatista commented Aug 23, 2018

Actually, I think the correct version of this file is this:

module App {
"use strict";
interface IMyAppController {
values: string[];
errorMessage: string;
isVisibleErrorMessage: boolean
}
export class MyAppController implements IMyAppController {
$onInit = () => { }; // this was missing and it's required ( at least, to make this file ready to compile )
errorMessage: string = "";
isVisibleErrorMessage: boolean = false;
values: string[] = [];
static $inject: string[] = ["$http", "$window"];
constructor(private $http: ng.IHttpService, private $window: ng.IWindowService) {
this.getValues();
}
private getValues(): void {
this.$http.get("/api/sample")
.then((response: ng.IHttpPromiseCallbackArg<string[]>) => {
this.isVisibleErrorMessage = false;
this.values = response.data;
})
.catch(((reason: ng.IHttpPromiseCallbackArg<string[]>) => {
this.isVisibleErrorMessage = true;
this.errorMessage = reason.statusText;
return this.values;
}));
}
}
angular.module("app").controller("MyAppController", MyAppController);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment