我是Angular的初学者,当我在他的路线上时,我尝试将菜单标记为已选中,我目前正在尝试使用$ location进行此操作,这是我的plunkr:http://plnkr.co/edit/XsZDkKfzfJCGO1MPRWx5?p=preview

angular.module('myApp', ["directives.sidebar"])

angular.module("directives.sidebar", []).directive("sidebar", ['$location', function($location) {
  return {
    restrict: "E",

    link: function(scope, element, attrs) {
      element.on("click", function() {
        var sidebar = $(".nav-sidebar")
        sidebar.toggleClass("is-open");
      });

      var currentUrl = $location.path()
      switch(currentUrl) {
        case "/equipments":
          var navEquipments = $('li.nav-item')[0]
          $(navEquipments).addClass('nav-item--selected')
          break;
        case "/softwares":
          var navSoftwares = $('li.nav-item')[1]
          $(navSoftwares).addClass('nav-item--selected')
          break;
        case "/transducers":
          var navTransducers = $('li.nav-item')[2]
          $(navTransducers).addClass('nav-item--selected')
          break;
        case "/exams":
          var navExams = $('li.nav-item')[3]
          $(navExams).addClass('nav-item--selected')
          break;
      }

    }
  };
}])


你们可以给我一些如何做的提示吗?

最佳答案

解决您的问题。
使用方式:

switch(currentUrl) {
    case "/equipments":
      var navEquipments = angular.element(document.querySelectorAll('li.nav-item')[0]);
      navEquipments.addClass('nav-item--selected')
      break;
    case "/softwares":
      var navSoftwares = angular.element(document.querySelectorAll('li.nav-item')[1]);
      navSoftwares.addClass('nav-item--selected')
      break;
    case "/transducers":
      var navTransducers = angular.element(document.querySelectorAll('li.nav-item')[2]);
      navTransducers.addClass('nav-item--selected')
      break;
    case "/exams":
      var navExams = angular.element(document.querySelectorAll('li.nav-item')[3]);
      navExams.addClass('nav-item--selected')
      break;
  }


see this link

09-28 03:19