2017-01-09 5 views
1

angular2 앱에 typescript이 있고 나는 ng2-dragula을 사용하고 있습니다. 다음예외 : 정의되지 않은 'addInstrumentToStage'속성을 읽을 수 없습니다.

constructor( private dragulaService: DragulaService, 
       private audioFinderService:AudioFinderService) {} 

및 ngOnInit() : 여기서 생성자이다

public ngOnInit(): any { 

     this.dragulaService.setOptions('second-bag', { 

        moves: function (el, container, handle) { 

           var file=this.audioFinderService.audioGetter(); 
           this.audioFinderService.removeFromStage(); 

및 라인 this.audioFinderService.audioGetter();는 불평 :

error_handler.js:50 EXCEPTION: Cannot read property 'audioGetter' of undefined 

난의 의존성 주입을 만들려고 생성자는 알지만 인식하지 못하는 것 같습니다. audioFinderService

여기 AudioFinderService에 대한 유일한 이상한 것은, 또 다른 서비스를 주입되어 있다는 것입니다 AudioFinderService

@Injectable() 
export class AudioFinderService { 
     constructor(private playService:SelectedInstrumentsService,private http:Http) { } 
    } 

입니다. 따라서 중첩 된 종속성 주입이 실패했다고 생각하십니까? 대신 지방 화살표 synthax의 function, 당신은 페이지로 다스 려하는 this을 잃어 가고 사용하는 경우

답변

2

변경

moves: function (el, container, handle) {//<-- 'this' will refer to the function object 

           var file=this.audioFinderService.audioGetter(); 
           this.audioFinderService.removeFromStage(); 

moves: (el, container, handle)=> {//<-- 'this' will refer to the page 

           var file=this.audioFinderService.audioGetter(); 
           this.audioFinderService.removeFromStage(); 

에 문제가 있습니다. How to access the correct `this` inside a callback?

을 :

은 당신이 올바른 this을 참조하는 방법이 좋은 답변을 살펴 보도록하는 것이 좋습니다

관련 문제