Angular md-nav-bar stuff doesn't work properly with ui-router

I played with angular and ui-router stuff.
Here is the 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>

Problem: eventhough md-selected-nav-item="'browse'"allows an application to activate a tab browseby default, it does not show the status of router-ui: projects.browse.
But when we click on the tab browse, it appears. The problem is that the routing function does not work when the page loads.

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>"
  });

Obviously I'm missing something. pen

+4
source share
1 answer

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);
})
+4

Source: https://habr.com/ru/post/1659307/


All Articles