When Using Typescript+angularjs the api result is not displaying in html -
below controller , html: when make api call in controller storing result in result variable, now,that result variable when trying print in html not printing anything, have used 1 more variable name in controller printing in html when print it. when try print result variable in controller printing data in console. so, getting data in variable correctly. but, why not printing in html. please help.
controller :
module typescript { var my_app = angular.module('my_app', []); export class test { public name: any; constructor(private $http: ng.ihttpservice) { this.name = "test data"; this.getdata(); } getdata() { return this.$http.get('facilities.json').success(function(response) { this.result = response; console.log(this.result); }) } } test.$inject = ['$http']; myapp.controller("test", test); }
any appreciated.
you have careful this
semantics. in code:
return this.$http.get('facilities.json').success(function(response) { this.result = response; console.log(this.result); })
this
not referring test
instance, instead whatever context function running in. instead this:
return this.$http.get('facilities.json').success(response => { this.result = response; console.log(this.result); })
now points test
instance. read this more information new use of this
. still have define result
property somewhere though :)
good luck.
Comments
Post a Comment