Nativescript Angular拍摄后无法将图像分配给属性

问题描述 投票:-1回答:1

我正在使用nativescript-camera捕获我的android应用程序中的图像,但我总是得到TypeError:无法设置未定义的属性'imageSource'。我正在按照here指南进行操作,我的所有相机代码似乎都是正确的,并且我不确定自己在哪里做错了。

export class SocialRegistrationComponent implements OnInit {

    imageSource: ImageSource;

    constructor() {}

    ngOnInit() {
    }

    capturePicture() {

        const options = {
        saveToGallery: false,
        allowsEditing: false,
        format: 'png'
        };

        camera.requestPermissions().then(
            function success() {
                camera.takePicture(options)
                .then((capturedImageAsset) => {

                    let imageSource = new imageSourceModule.ImageSource;

                    imageSource.fromAsset(capturedImageAsset)
                    .then((capturedImageSource: ImageSource) => {

                        console.log(capturedImageSource); // Display correctly
                        this.imageSource = capturedImageSource; // Error here, this.imageSource undefined
                    });
                }).catch((err) => {
                    console.log('Error -> ' + err.message);
                });
            }, 
            function failure() {
                // failed
            }
        );

    }

}
angular android-camera nativescript nativescript-angular
1个回答
0
投票

上下文(this)将不会指向成功函数中的Angular组件。您必须使用箭头功能或将上下文保留在局部变量中。

success() => {
  // Using arrow function retains the context
  .... 
}
© www.soinside.com 2019 - 2024. All rights reserved.