2016-10-08 12 views
3

である私は私の角のアプリケーションでツールチップを作るために、次のディレクティブのコードを使用しています:

//Creates a directive that is used to display tooltips throughout the application 
app.directive('tooltip', function(){ 
    return { 
     restrict: 'A', 
     link: function(scope, element, attrs){ 
      $(element).hover(function(){ 
       // on mouseenter 
       $(element).tooltip('show'); 
      }, function(){ 
       // on mouseleave 
       $(element).tooltip('hide'); 
      }); 
     } 
    }; 
}); 

トラブルは、私が

を受けるということです$は定義されていません

コンソールにあります。今、私の取り組みは、これらがjQueryのメソッドであるということです。私はこのアプリ(これのみツールチップのために使用される場合は特に)にはjQueryを使用して回避しようとしているので、次のように、私はコードを変更することを決定した:第2のケースで

//Creates a directive that is used to display tooltips throughout the application 
app.directive('tooltip', function(){ 
    return { 
     restrict: 'A', 
     link: function(scope, element, attrs){ 
      document.getElementById(element).hover(function(){ 
       // on mouseenter 
       document.getElementById(element).tooltip('show'); 
      }, function(){ 
       // on mouseleave 
       document.getElementById(element).tooltip('hide'); 
      }); 
     } 
    }; 
}); 

を、エラーがI状態を受け取ります

私はエラーをクリアし、jQueryの使用を避けることができる方法

ヌル

のプロパティホバーを読み取ることができません。これは可能ですか?

編集:エラーをクリアするには、次のように私は、コードを変更した

//Creates a directive that is used to display tooltips throughout the application 
app.directive('tooltip', function(){ 
    return { 
     restrict: 'A', 
     link: function(scope, element, attrs){ 
      element.on('mouseenter', function() { 
       element.tooltip('show'); 
      }); 
      element.on('mouseleave', function() { 
       element.tooltip('hide'); 
      }); 
     } 
    }; 
}); 

しかし、今element.tooltip is not a function。ここで勝つことは可能ですか? :)

+1

FYIでは、DOM要素には '.hover()'メソッドがありません。これはjQueryメソッドです。 – jfriend00

+0

「要素」の説明については、以下の回答を参照してください。これはjqLit​​eオブジェクトです。 – MadPhysicist

答えて

2

Angular Directive documentationによると、elementパラメータはすでにjqLit​​e、ラップされたオブジェクトであるので、それに$()またはdocument.getElementById()を呼び出す必要はありません。 elementオブジェクトで直接操作を実行できます。

+1

このjqLit​​e獣とは何ですか? – MadPhysicist

+1

@MadPhysicist Angularがデフォルトでインクルードする軽量のjQueryライクなライブラリです。詳細はこちらをご覧ください:https://gist.github.com/esfand/9638882 –

+0

[AngularJS angular.element APIリファレンス - jqLit​​e](https://docs.angularjs.org/api/ng/function/angular .element#angular-s-jqlite)を参照してください。 – georgeawg

関連する問題