javascript - How to make Ajax Call using Angular with ES6 and Babel JS -


i using framework called angular-webpack-seed, contains, webpack, es2016, want make simple ajax call using angular old way doesn't work.

export default class homecontroller {   constructor($http) {     'nginject';     this.currentplatform = '';     this.platforms = ["web portal", "roku", "android", "iphone"];   }    makeajaxcall() {     // simple request example:     $http({       method: 'get',       url: '/index.html'     }).then(function successcallback(response) {       // callback called asynchronously       // when response available     }, function errorcallback(response) {       // called asynchronously if error occurs       // or server returns response error status.     });   }    setplatform(platform) {     this.currentplatform = platform;     this.makeajaxcall();   }    isselected(platform) {     return this.currentplatform == platform;   } } 

please tell me what should configure $http work, console prompts $http not defined.

$http not defined because visible constructor.

you have affect 'this' make available method class :

  constructor($http) {     'nginject';     this.currentplatform = '';     this.platforms = ["web portal", "roku", "android", "iphone"];     this.$http = $http;   }    makeajaxcall() {     // simple request example:     this.$http({       method: 'get',       url: '/index.html'     }).then(function successcallback(response) {       // callback called asynchronously       // when response available     }, function errorcallback(response) {       // called asynchronously if error occurs       // or server returns response error status.     });   } 

on project, prefer use object.assign, shorter way it.

constructor($http, $location, $sce) {     // affect parameters     object.assign(this, {$http, $location, $sce});  } 

hope :)


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -