Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular NgZone或ChangeDetectorRef在离子3中不工作_Angular_Ionic3 - Fatal编程技术网

Angular NgZone或ChangeDetectorRef在离子3中不工作

Angular NgZone或ChangeDetectorRef在离子3中不工作,angular,ionic3,Angular,Ionic3,我正在我的ionic 3 iOS应用程序中使用cordova插件文档扫描插件,它允许我使用一些附加功能拍照。一切正常,我在成功回调中得到了fileURI(file:///...) 拍照后,我无法更新DOM以在my view home.html中显示图像。我还试图更新一个简单的文本(测试),但它也不起作用 我已经用ngZone和ChangeDetectorRef试过了。两者都给了我: Error in Success callbackId: Scan683272023 : TypeError: n

我正在我的ionic 3 iOS应用程序中使用cordova插件文档扫描插件,它允许我使用一些附加功能拍照。一切正常,我在成功回调中得到了fileURI(file:///...)

拍照后,我无法更新DOM以在my view home.html中显示图像。我还试图更新一个简单的文本(测试),但它也不起作用

我已经用ngZone和ChangeDetectorRef试过了。两者都给了我:

Error in Success callbackId: Scan683272023 : TypeError: null is not an object (evaluating 'this.test = ('changed')')
home.html

<p>{{test}}</p>
<img [src]="image | safePipe: 'url'" #imageResult />

我找到了一个有效的解决方案:

takePicture() {

    this._images = true;

    scan.scanDoc(0,
      (onSuccess) => {
        this.image = onSuccess;
        this.imageResult.nativeElement.src = this.image;
      });

  }

您有“this”问题-如果在回调中调用onSuccess方法,则它没有指向主组件

要修复此问题,您可以通过fat arrow执行分配:

onSuccess=()=>{您的代码在这里}

你的答案之所以有效,是因为你使用了fat-arrow函数,它不会创建新的“this”上下文

takePicture() {

    this._images = true;

    scan.scanDoc(0,
      (onSuccess) => {
        this.image = onSuccess;
        this.imageResult.nativeElement.src = this.image;
      });

  }