2017-02-07 23 views
1

私のウェブショップでは、moとCSSとAngularJSのバッジカウンターを作っています。 Angularには、badgeDirectiveを実装するHTML要素に属性を追加または削除するディレクティブがあります。このように:CSSコンテンツattr()が設定されていますが、表示されません

<md-icon class="material-icons" badge-directive="cart.length" 
aria-label="Cart" >remove_shopping_cart</md-icon> 

CSS:

.badge-directive[badge-counter]:after { 
    background: #5da2e2; 
    border-radius: 7px; 
    color: #fff; 
    content: attr(badge-counter); 
    font-size: 10px; 
    font-weight: 600; 
    height: 14px; 
    line-height: 13px; 
    right: -1px; 
    padding: 0 6px; 
    position: absolute; 
    width: 12px; 
    text-align: center; 
    top: 13px; 
} 

私は、サイトを実行すると、私はバッジが所定の位置にあることがわかりませんが、兆候

angular.module('myApp') 
    .directive('badgeDirective', function($timeout){ 
    return function(scope, elem, attrs) { 
     $timeout(function() { 
      elem.addClass('badge-directive'); 
     }, 0); 
     scope.$watch(attrs.badgeDirective, function(newVal) { 
      if (newVal > 0) { 
       elem.attr('badge-counter', newVal); 
      } else { 
       elem.removeAttr('badge-counter'); 
      } 
     }); 
    } 
}); 

HTMLは次のようになります実際のカウンタ:

Badge counter without an actual counter that shows length of 'cart'

私は、デバッガで詳しく見て取り、これを見つけた:

The cart length is there. But not showing!

誰かがこれで私を助けることができますか? なぜ値が表示されないのかわかりません...

+3

私は100%確実ではないですが、あなたが使用している場合は、カスタムのHTML要素の属性、それは彼らが「「データ - 」で始まることは必要ではないでしょうか? "data-badge-counter"のように。 – MateBoy

+0

あなたは直面している問題をデモするためのサンプルを作成できますか?私はcodepenであなたのCSSを試しました、それはあなたのコードの問題ではないので、正常に動作します。 http://codepen.io/anon/pen/ZLjRXy – dommmm

+0

あなたの属性が実際に生成されていることを確認するために、カウンタ値の代わりにテキストを設定しようとしましたか?値が0の場合、そこにはありません –

答えて

1

ここではどのように動作させることができます。

.material-icons[badge-directive]:after { 
 
    background: #5da2e2; 
 
    border-radius: 7px; 
 
    color: #fff; 
 
    content: attr(badge-counter); 
 
    font-size: 10px; 
 
    font-weight: 600; 
 
    height: 14px; 
 
    line-height: 13px; 
 
    right: -1px; 
 
    padding: 0 6px; 
 
    position: absolute; 
 
    width: 12px; 
 
    text-align: center; 
 
    top: 13px; 
 
} 
 

 

 

 
.material-icons { 
 
    display: block; 
 
    position: relative; 
 
    margin-bottom: 1rem; 
 
}
<md-icon class="material-icons" badge-directive="cart.length" 
 
aria-label="Cart" badge-counter="1">remove_shopping_cart</md-icon> 
 

 
<md-icon class="material-icons" aria-label="Cart" >remove_shopping_cart</md-icon>

+0

アドバイスありがとうございますが、機能しませんでした。しかし、いくつかのテストを通じて、私はそれが動作するように管理しました。 'md-icon'は 'md-button'の中にあります。だから私は代わりにボタンにバッジディレクティブを移動しました。そしてそれは働いた!何かは、 'md-icon'はカウンターを置くのに適切な場所ではないと私に伝えます。 – ClydeFrog

関連する問題