无法从函数访问 angular2 服务

Unable to access angular2 services from a function

无法从 else 条件下的 function() 访问我的服务。我得到的只是在浏览器终端中 "TypeError: this._hitoService is undefined"。我需要在执行 else 条件时使用服务获取数据。我该怎么做?

@Component({
  selector: 'app-time-line',
  templateUrl: './time-line.component.html',
  styleUrls: ['./time-line.component.css'],
  providers: [HitoService],
  entryComponents: [FormHitoComponent]
})
export class TimeLineComponent implements OnInit, OnChanges {
  @Input() calbuscador: String;

 nom_cal1: any;
 hito1: IHito[];
   hito: any;

  constructor(private _hitoService: HitoService) { }

  ngOnInit() {


 }

 ngOnChanges(){
       if (typeof this.calbuscador === "undefined"){
           swal("Atencion!", "Busca un calendario con el buscador del Side Menu", "error")
       } 
       else{
         this.nom_cal1 = this.calbuscador;
            swal({
                title: "Are you sure?",
                text: "Se cargara el calendario : " + this.calbuscador,
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes, I am sure!',
                cancelButtonText: "No, cancel it!",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    swal({
                        title: 'Cargando timeline!',
                        text: 'Cargando el Calendario en el Timline con sus hitos!',
                        type: 'success'
                    }, function() {
                           this._hitoService.getHitos()
                          .subscribe(hito1 =>{ 
                            this.hito1 = hito1
                            console.log(this.hito1); // defined!
                            console.log("nomcal1" + this.nom_cal1);
                            if (typeof this.nom_cal1 === "undefined"){
                                swal("Atencion!", "Busca un calendario con el buscador del Side Menu")
                            }else{
                            drawtimeline1_1(this.hito1, this.nom_cal1);
                            }
                          }); 

                    });

                } else {
                    swal("Cancelled", "No se cargara el Timeline :)", "error");
                }
            });
       }



 }

很多例子都有同样的问题。根据经验,永远不要在 类 和 TypeScript 中使用 function 关键字。这会将 this 上下文替换为当前函数范围的上下文。始终使用 () => {} 表示法:

swal({
        ...
}, (isConfirm) => {

});