지시문에서 컨트롤러 기능을 호출하려고하는데 "this"컨텍스트 문제가 발생합니다. 함수가 지시문을 통해 호출되면 logSerivce에 액세스 할 수 없습니다. 여기via 유형 스크립트를 통해 Angularjs 컨트롤러가 지시문에서 컨트롤러 기능을 호출합니다.
컨트롤러 클래스 : 여기
class MainController implements IMainScope {
static $inject = ["LogService"];
constructor(private logService:ILogService) { }
sayHello(message:string) {
console.log("MainController sayHello", message);
// Cannot read property 'logService' of undefined if gets call from directive
this.logService.log(message);
}
}
지시어 클래스 : http://embed.plnkr.co/Ov7crFZkkjDPzilX2BmL/
고마워요! 그것은 작동! 유감 스럽지만 데모 링크에는 업데이트 버전이 표시되지 않습니다. 나는이 호출을 변경했다 :'test-directive test- fn = "vm.sayHello (message)"'와 지시문의 호출. 'scope.testFn ({message : scope.firstName});'[여기에 새로운 데모 버전이 있습니다.] (https://embed.plnkr.co/nd54cRZqMDtnVM4XXhog/). 다시 한번 고마워! – stevo
@stevo 완전히 업데이트하는 걸 잊었습니다.) 감사합니다 –
첫 번째 코드를'fn = "vm.sayHello (message)'로 업데이트하고 싶을 수도 있습니다. – stevo