编程语言
首页 > 编程语言> > javascript – Angular Material的md-nav-bar与ui-router无法正常工作

javascript – Angular Material的md-nav-bar与ui-router无法正常工作

作者:互联网

我正在玩角形材料和ui-router.
这是pen.

HTML:

<md-nav-bar md-selected-nav-item="'browse'" nav-bar-aria-label="navigation links">
  <md-nav-item md-nav-sref="projects.browse" name="browse">Browse</md-nav-item>
  <md-nav-item md-nav-sref="projects.settings" name="settings">Settings</md-nav-item>
</md-nav-bar>

问题是,尽管md-selected-nav-item =“’browse’”允许应用程序默认激活浏览选项卡,但它没有显示ui-router状态:projects.browse.
但是当我们点击它显示的浏览选项卡时.问题是当页面加载up.js时,路由功能不起作用

$stateProvider
  .state({
            name: "projects",
            url: "/projects",
            template: "<h4>this is project page:</h4> <ui-view></ui-view>"
  })
  .state({
            name: "projects.browse",
            url: "/browse",
            template: "<h3>Browse</h4>"
  })
  .state({
            name: "projects.settings",
            url: "/settings",
            template: "<h3>Settings</h4>"
  });

显然我错过了一些东西.
pen

解决方法:

阅读docs似乎md-nav-sref在单击选项卡时处于活动状态:

enter image description here

这是一个工作 – CodePen

标记

<div ng-cloak ng-app="MyApp" ng-controller="MyController">
  <!-- Navbar -->
  <md-content class="md-padding">
    <md-nav-bar md-selected-nav-item="selectedItem" nav-bar-aria-label="navigation links">
      <md-nav-item md-nav-sref="projects.browse" name="browse">Browse</md-nav-item>
      <md-nav-item md-nav-sref="projects.settings" name="settings">Settings</md-nav-item>
    </md-nav-bar>

    <ui-view></ui-view>
  </md-content>
</div>

JS

angular.module('MyApp', ['ui.router', 'ngMaterial', 'ngMessages']);

angular.module('MyApp')

.config(function($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) {

    // for handling trailing slashes
    $urlMatcherFactoryProvider.strictMode(false);

    // for any unmatched url, redirect to the default.
  // NOTE: I can't adjust this value to fix this issue.
  //       setting this to "/projects/browse" may fix this issue,
  //       but I cant go with that.
    $urlRouterProvider.otherwise("/projects");

  $stateProvider
  .state({
            name: "projects",
            url: "/projects",
      template: "<h1>this is project page:</h1> Eventhough the browse tab is selected, content of it isn't showing when the page reloads. <ui-view></ui-view>"
  })
  .state({
            name: "projects.browse",
            url: "/browse",
      template: "<h3>Browse</h4>"
  })
  .state({
            name: "projects.settings",
            url: "/settings",
      template: "<h3>Settings</h4>"
  });

})

.controller("MyController", function ($scope, $state) {
  var initTab = "browse";
  $scope.selectedItem = initTab;
  $state.go("projects." + initTab);
})

标签:javascript,angularjs,angular-ui-router,angular-material,html
来源: https://codeday.me/bug/20190623/1267144.html