gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:15 25 4
gpt4 key购买 nike

我在玩angular materialui-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'"使应用程序能够激活 browse默认情况下,它不显示 ui-router 状态:projects.browse .
但是当我们点击 browse它显示的选项卡。问题是页面加载时路由功能不起作用。

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 - Angular Material 的 md-nav-bar 无法与 ui-router 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com