Phonegap pictureSource.PHOTOLIBRARY不适用于角度

时间:2016-01-18 10:29:10

标签: javascript angular cordova

没有角度,它的工作正常:

<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Album</button>
<script type="text/javascript">
function onPhotoURISuccess(imageURI) {
  alert(imageURI)
}

function getPhoto(source) {
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
    destinationType: destinationType.imageURI,
    sourceType: source });
}

function onFail(message) {
  alert('Failed because: ' + message);
}
    </script>

但是当我尝试使用angular 2时,我的代码不起作用:

<button (click)="getPhoto(pictureSource.PHOTOLIBRARY)">From Photo Album</button>

getPhoto(source) {
    alert(111);
    navigator.camera.getPicture(this.onSuccess, this.onFail, { quality: 50,
        destinationType: navigator.camera.DestinationType.imageURI,
        sourceType: source });
}
onSuccess(imageData) {
    this.photo = imageURI;
}
onFail(message) {
    alert('Photo not loaded, because: ' + message);
}

当我点击按钮时,getPhoto功能不起作用。因为函数中有pictureSource.PHOTOLIBRARY。

1 个答案:

答案 0 :(得分:0)

这个API可能没有被Angulars区域修补。尝试明确使用区域

constructor(private zone: NgZone) {}

getPhoto(source) {
  alert(111);
  zone.run(() => {
    navigator.camera.getPicture(this.onSuccess, this.onFail, { quality: 50,
      destinationType: navigator.camera.DestinationType.imageURI,
      sourceType: source });
  });
}

(不确定语法是否正确,我不是TS开发人员)