2016-03-15 26 views
1

我無法使鏈接功能在ES6中工作。點擊事件根本沒有上下文傳遞 - 我錯過了一些明顯的東西嗎?角es6,這個,範圍和埃爾在jqlite點擊事件中都未定義

export function TbeDirective() { 
     'ngInject'; 

     let directive = { 
     restrict: 'E', 
     templateUrl: 'app/main/directives/template.html', 
     controller: TheController, 
     controllerAs: 'vm', 
     bindToController: true, 
     link: function(scope, el, attr, vm){ 

      el.bind('keypress', function(){ 
      var x = this.firstChild; //in the console this is the element, 
            //but when the code runs it is undefned 
      console.log(this);  //undefined 
      console.log(TheDirective); //undefined 
      console.log(el);   //undefined 
      console.log(vm);   //undefined 
      }); 
     } 
     }; 

     return directive; 
    } 

    class TheController { 
     constructor ($scope) { 
     'ngInject'; 

     } 
    } 
    } 
+0

您正在使用沒有上下文的箭頭函數。 – Bergi

+0

在沒有箭頭功能的情況下不起作用 – userqwert

+1

如果您希望將'this'作爲事件處理程序中的元素,則處理程序不能是箭頭函數。有'鏈接'的箭頭功能也沒有意義。 – zeroflagL

回答

1

TheController必須在使用它的地方定義。

class我不會被吊起來,我知道。