0

角度cliアプリでアイテムを削除しようとしています。私は警告として甘い警告を使用して、私はリストから項目を削除したい。ここにそのコードがあります。このコードはタイスクリプトファイルです。角2 - スウィートアラートコールバック機能が動作しません

import { AuthenticationService } from '../../../services/authentication.service'; 
declare var swal: any; 
export class AdminUsersComponent implements OnInit { 

    constructor(
     private authService: AuthenticationService, 
    ) { } 

    deleteUser(id) { 
     let userData = { 
      user_id: id 
     }; 
     swal({ 
      title: "Are you sure?", 
      text: "You will not be able to recover this!", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Yes, delete it!", 
      closeOnConfirm: false 
     }, function(){ 
      this.authService.deleteUser(userData).subscribe(data => { 
       // response 
      }); 
     }); 

    } 
} 

問題を削除すると「this.authserivce」が定義されていないというエラーが表示されます。私は確認として甘い警告を使用しない場合、それは正常に動作しています。私はコールバック関数でパラメータを渡す必要があると推測しているが、私は何を渡す必要があるか分からない。だから私はこれを解決する方法は?

+0

は、あなたが全体のコードを送ることができますか? 'authService.deleteUser'とは何ですか? – Manav

+0

@manav私はコードを更新しました。見てみましょう – parth

+0

多分sweetalertは 'this'を上書きしていますか? – Manav

答えて

0

第一ソリューション:使用arrow functionため、独自のthis

swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }).then((result) => { 
     if (result.value) { 
      this.authService.deleteUser(userData).subscribe(data => { 
       // response 
      }); 
     } 
    }); 

第二溶液による機能発現バインドthis

let that = this; 
swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }).then(function(result) { 
     if (result.value) { 
      that.authService.deleteUser(userData).subscribe(data => { 
       // response 
      }); 
     } 
    }); 
+1

ありがとうございました。それは今働いている。 – parth

関連する問題