猿问

angularjs中的随页面变化的导航

项目现在用的是angular(1.x)。自己写了一个导航指令,但是导航在路由逻辑之外

在ng-view外面,现在想让导航条随着页面url的变化,有一个指示(就是表示在当行的哪一个栏目)。

https://img2.mukewang.com/5bcae3fa00019b2307490105.jpg

我在推荐就推荐变灰,我在视频就视频变灰

我的办法是每一个控制器的最前面写一个 scope.selectNav变成对应的数字(例如在视频selectNav为3),然后在导航指令中用ng-class控制,可是由于指令写在控制器之外,每次路由切换指令不会重载,有没有什么办法可以让selectNav一改变,导航指令的样式就跟着改变


牧羊人nacy
浏览 767回答 1
1回答

慕虎7371278

看看这是不是你要的:nav.html<!DOCTYPE&nbsp;html><html&nbsp;lang="en"><head> &nbsp;&nbsp;&nbsp;&nbsp;<meta&nbsp;charset="UTF-8"> &nbsp;&nbsp;&nbsp;&nbsp;<title>Nav</title> &nbsp;&nbsp;&nbsp;&nbsp;<link&nbsp;rel="stylesheet"&nbsp;href="css/nav.css"></head><body&nbsp;ng-app="navApp"&nbsp;ng-controller="navCtrl"> &nbsp;&nbsp;&nbsp;&nbsp;<div&nbsp;class="container"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<nav-bar></nav-bar> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div&nbsp;ui-view&nbsp;class="content"></div> &nbsp;&nbsp;&nbsp;&nbsp;</div></body><script&nbsp;src="angular/angular.js"></script><script&nbsp;src="angular-ui-router/release/angular-ui-router.js"> &nbsp;&nbsp;&nbsp;&nbsp;</script><script&nbsp;src="js/nav.js"></script></htmltab.html<ul class="nav clearfix">&nbsp; &nbsp; <li ng-repeat="tab in tabs"><a ui-sref="{{tab|lowercase}}" ui-sref-active="active">{{tab}}</a></li></ul>nav.jsangular.module('navApp',['ui.router'])&nbsp; &nbsp; .config(function($stateProvider,$urlRouterProvider){&nbsp; &nbsp; &nbsp; &nbsp; $urlRouterProvider.otherwise('/home');&nbsp; &nbsp; &nbsp; &nbsp; $stateProvider&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .state('home',{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/home',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<div>Welcome to home page!</div>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .state('recommend',{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/recommend',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<div>recommend</div>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .state('videos',{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/videos',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<div>videos</div>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .state('shopping',{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/shopping',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<div>shopping</div>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })&nbsp; &nbsp; .directive('navBar', function(){&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restrict: 'EA',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope:{},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; templateUrl: '../views/tab.html',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller: function($scope){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.tabs = ['Home','Recommend','Videos','Shopping'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; .controller('navCtrl', function($scope){&nbsp; &nbsp; })app.jsvar express = require('express');var bodyParser = require('body-parser');var app = express();app.use(bodyParser.json());app.use(bodyParser.urlencoded({&nbsp; &nbsp; extended: true}));app.use(express.static('./public'));app.use(express.static('./node_modules'));var PORT = process.env.PORT || 8000;app.listen(PORT, ()=>{&nbsp; &nbsp; console.log('server running on port: ' + PORT);});app.get('/nav', (req,res)=>{&nbsp; &nbsp; res.sendFile(__dirname + '/public/views/nav.html');});nav.css*{padding: 0;margin: 0;color: #333;}ul{list-style: none;}a{&nbsp; &nbsp; color: inherit;&nbsp; &nbsp; text-decoration: none;}.clearfix{zomm:1;clear: both;}.clearfix:after{&nbsp; &nbsp; content: "";&nbsp; &nbsp; display: block;&nbsp; &nbsp; clear: both;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; height: 0;}.container{&nbsp; &nbsp; margin: 100px auto;&nbsp; &nbsp; width: 400px;&nbsp; &nbsp; border: 1px solid #ddd;}.nav{margin-bottom: 10px;border-bottom: 1px solid #ddd;}.nav li{&nbsp; &nbsp; float: left;&nbsp; &nbsp; width: 25%;&nbsp; &nbsp; box-sizing: border-box;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; border-right: 1px solid #ddd;}.nav li:last-child{border-right: 0;}.nav li a{display: block;padding: 5px 0;}.nav li a:hover{background-color: lightgray;}.content{&nbsp; &nbsp; min-height: 200px;}.active{background-color: lightgray;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答