【发布时间】:2018-06-07 04:52:54
【问题描述】:
我在从 Controller 中的服务访问数据时遇到问题。 这是我的服务的文件代码:
import {IHttpService} from 'Angular';
export class MyService {
public static $inject = ['$http'];
constructor(private $http:IHttpService, private getItems){
this.getItems= function() {
this.$http.get('items.json').then(function(response){
if(err){
console.error(err);
}
console.log(response.data);
return response.data;
});
}}}
以及Controller的文件代码:
import {MyService} from './MyService';
export class MyController {
public:getData;
static inject: Array<string> = ['$http', '$scope', 'MyService'];
constructor(private $http:ng.IHttpService, private $scope:any, private MyService:any){
this.getData = function(){
MyService.getItems.then(function (data) {
this.getItems = data;
console.log(data);
});
}
}
谁能解释我的代码有什么问题?谢谢。
【问题讨论】:
标签: angularjs typescript angularjs-http