2013-10-07 37 views

回答

3

$(this)將取決於方法的上下文中被執行,將$(element)始終指的是該指令被附接到。

這裏是一個人爲的例子

module.directive('myDirective', [function() { 
    return { 
     template: '<div><button id="btn">Click Me</button></div>', 
     restrict: 'E', 
     link: function(scope, element, attrs, controller) { 
       $("#btn").on('click', function() { 
        // $(this) != $(element) 
        // $(this) is the button element from the template 
        // $(element) is the directive element 
       }); 
      } 
     } 
}]); 
+0

美麗!謝謝 –

1

由於角度js在選擇器的引擎下使用jqlite(jquery的最小版本),所以它在角度js中也可能與使用不同名稱的角色相同。

參見: AngularJS DOM selector

+0

野趣答案更多地瞭解這個關鍵字在JavaScript:http://stackoverflow.com/questions/3127429/javascript-this-keyword – webcoder

相關問題