1 レベルのナビゲーション/メニューの場合は、"localhost/#/user" のような URL
私は使用できることを知っています:
<ul>
<li ng-class="{active: isActive('/user')}><a href="#/user'>User</a></li>
</ul>
そしてコントローラーで
$scope.isActive = function (path) {
if ( path == $location.path() )
return true;
return false;
};
url が「localhost/#/user」の場合、「active」が追加されます。
それでも、2 レベルのメニューになると、「localhost/#/user/orders」のような URL
<ul>
<li>
<a href="#/user'>User</a>
<ul>
<li><a href="#/user/orders'>Orders</a></li>
</ul>
</li>
</ul>
URLに基づいてアイテム「注文」とその親「ユーザー」の両方に「アクティブ」クラスを追加するにはどうすればよいですか? (両方をハイライトできるように)
前もって感謝します。
アップデート:
ありがとう、それは今働いています:D
@Nikos Paraskevopoulos、@piatek、@Chandermaniありがとう
コードは十分ではありませんが、CoffeeScript で書かれた私の最終的な作業コードは次のとおりですが、動作します:)
.directive('highlightActive', () ->
return {
restrict: "A"
controller: [
'$scope', '$element', '$attrs', '$location'
($scope, $element, $attrs, $location) ->
links = $element.find('a')
path = () ->
return $location.path()
highlightActive = (links, path) ->
path = '#' + path
angular.forEach(links, (link) ->
$link = angular.element(link)
$li = $link.parent('li')
href = $link.attr('href')
if ($li.hasClass('active'))
$li.removeClass('active')
if path.indexOf(href) is 0
$li.addClass('active')
)
highlightActive(links, $location.path())
$scope.$watch(path, (newVal, oldVal) ->
if newVal is oldVal
return
highlightActive(links, $location.path())
)
]
}
)