扫码关注官方订阅号
angularjs使用路由跳转到某个view的时候,如何执行js来修改一个导航菜单某一项为激活状态,下面是我现在的代码
大概代码:
html<body ng-app="app"> <ul> <li><a href="#/" ng-class="{active:path=='/'}">index</a></li> <li><a href="#/a" ng-class="{active:path=='/a'}">a</a></li> <li><a href="#/b" ng-class="{active:path=='/b'}">b</a></li> </ul> <p ng-controller="main"> <p ng-view></p> </p> <script type="text/javascript"> var app = angular.module('app', ['ngRoute']); app.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/', { template: '/index', controller: 'test' }) .when('/a', { template: '/a', controller: 'test' }) .when('/b', { template: '/b', controller: 'test' }) .otherwise({ redirectTo: '/' }); }]); app.run(['$rootScope', '$location', function($rootScope, $location) { $rootScope.$on('$routeChangeSuccess', function(newV) { $rootScope.path = $location.path() }); }]); app.controller('main', ['$scope', function($scope){ }]); app.controller('test', function() {}); </script> </body>
html
<body ng-app="app"> <ul> <li><a href="#/" ng-class="{active:path=='/'}">index</a></li> <li><a href="#/a" ng-class="{active:path=='/a'}">a</a></li> <li><a href="#/b" ng-class="{active:path=='/b'}">b</a></li> </ul> <p ng-controller="main"> <p ng-view></p> </p> <script type="text/javascript"> var app = angular.module('app', ['ngRoute']); app.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/', { template: '/index', controller: 'test' }) .when('/a', { template: '/a', controller: 'test' }) .when('/b', { template: '/b', controller: 'test' }) .otherwise({ redirectTo: '/' }); }]); app.run(['$rootScope', '$location', function($rootScope, $location) { $rootScope.$on('$routeChangeSuccess', function(newV) { $rootScope.path = $location.path() }); }]); app.controller('main', ['$scope', function($scope){ }]); app.controller('test', function() {}); </script> </body>
如果想看一些示例和文档的话 我的github上也有 学习资源:https://github.com/dolymood/AngularLearing 简单示例:https://github.com/dolymood/angular-example 下载包和文档 https://github.com/dolymood/angular-packages
预先在每个菜单项上放好激活状态的效果,但是不显示;
然后通过传特定的值到前台页面来让特定的激活状态显示;
比如
<span class="highlight" ng-show="tab == 1"></span> <span class="highlight" ng-show="tab == 2"></span> <span class="highlight" ng-show="tab == 3"></span> <span class="highlight" ng-show="tab == 4"></span>
你在controller里传相应的tab值就行了
$scope.tab = 1 // 显示第一个为激活状态
或者用这个方法
<p ng-class='{active:isActive'> .active{ ... }
angular controller中 控制isActive的值true/false
<script type="text/javascript"> var mainapp = angular.module('mainapp', ['ngRoute']); mainapp.config(function(${routeProvider}) { ${routeProvider} .when('/', { templateUrl : 'marry.php?cid=123&view=home', controller : 'mainController' }) .when('/home', { templateUrl : 'marry.php?cid=123&view=home', controller : 'mainController' }) .when('/process', { templateUrl : 'marry.php?cid=123&view=process', controller : 'mainController' }) .when('/message', { templateUrl : 'marry.php?cid=123&view=message', controller : 'mainController' }); }); mainapp.controller('mainController', function(${scope}) { ${scope}.message = 'Everyone come and see how good I look!'; }); </script> <ul class="ul_nav"> <li ng-class="{active:path=='#/invitation'}"><a href="#/invitation" class="nav_01"></a></li> <li ng-class="{active:path=='#/map'}"><a href="#/map" class="nav_02"></a></li> </ul>
你好,我angularjs不太熟,我们项目只用了angularjs的路由功能,上面是我的代码,能帮我基于上面代码直接改好吗
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
大概代码:
如果想看一些示例和文档的话 我的github上也有
学习资源:https://github.com/dolymood/AngularLearing
简单示例:https://github.com/dolymood/angular-example
下载包和文档 https://github.com/dolymood/angular-packages
预先在每个菜单项上放好激活状态的效果,但是不显示;
然后通过传特定的值到前台页面来让特定的激活状态显示;
比如
你在controller里传相应的tab值就行了
或者用这个方法
angular controller中 控制isActive的值true/false
你好,我angularjs不太熟,我们项目只用了angularjs的路由功能,上面是我的代码,能帮我基于上面代码直接改好吗