내 angularJS 4 앱에서 장치 모션이라고하는 코드바 가속도계 플러그인을 사용하고 있습니다. API에는 getAcceleration (successCallback, errorCallback)이라는 함수 호출이 있습니다. 그래서 난 내 구성 요소에서이이 약속에서 콜백 값을 반환하려면 어떻게해야합니까?
@Injectable()
export class AccelerometerService {
private acc : any;
constructor(private winRef:WindowRef) {
this.acc = this.winRef.nativeWindow.plugins.navigator.accelerometer;
this.onSuccess = this.onSuccess.bind(this);
this.onError = this.onError.bind(this);
}
getAcceleration(){
return new Promise ((resolve, reject) =>{
resolve(this.acc.getCurrentAcceleration(this.onSuccess,this.onError));
});
}
onSuccess(acceleration){
return acceleration; // value that I want returned to my component
}
onError(){
return 'error';
}
}
과 같은 서비스를 만들어,이 그러나 응답이
this.accelerationService.getAcceleration().then((res) =>{
console.log(res); // res is undefined
})
어떻게 내가 할 수있는 정의되지는 onSuccess 콜백 함수의 반환 값을 얻기 위해 시도 할 이 문제를 해결 하시겠습니까? 이 같은
getCurrentAcceleration이 반환되지 않고 결과로 약속을 해결해야합니다. –
이것은 observalbes를 사용하여 해결해야합니다. – Nickolaus
@ Nikolaus 감사. 나는 관측 가능을 더 보게 될 것이다. – joejoeso