Service in Controller에서 데이터에 액세스하는 데 문제가 있습니다. Here`s 내 서비스의 파일 코드 :TypeScript에서 각도 1.6을 사용하는 서비스에서 데이터를 가져올 수 없습니다.
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`s 파일 코드 :
는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);
});
}
}
는 사람이 내 코드를 잘못 what`s 설명 할 수 있습니까? 감사.
하하! 당신은 빨리 형편이되었습니다. – Sajeetharan
형제 님, 고마워요.하지만 여전히 작동하지 않습니다. (( –
Downvoter, 왜 하향 투표를 하시겠습니까?) 감사합니다. –