[英]Bootstrap and Angular JS button issue
I have this bootstrap and Angular JS app. 我有这个引导程序和Angular JS应用程序。 I am using the following directive to show active navigation links: 我正在使用以下指令显示活动的导航链接:
mainControl.directive("myDataToggle", function(){
function link(scope, element, attrs) {
var e = angular.element(element);
e.on('click', function(){
e.parent().parent().children().removeClass('active');
e.parent().addClass("active");
})
}
return {
restrict: 'A',
link: link
};
});
Problem is this works fine in the nav, but if I have a button link that links to the separate sections of the app, the main nav at the top does not get updated To reflect which view I am in? 问题是,它在导航中正常工作,但是如果我有一个链接到应用程序各个部分的按钮链接,则顶部的主导航不会被更新以反映我所在的视图?
Any ideas? 有任何想法吗?
The problem is that you're setting the class when you're clicking the link, not when you are navigating. 问题在于,您是在单击链接时设置类的,而不是在导航时设置的。 I presume you're using the ngRoute. 我想您正在使用ngRoute。 If so you can try this approach: 如果是这样,您可以尝试这种方法:
Provide a name for your routes 提供您的路线名称
$routeProvider.
when('/', {
templateUrl: 'views/home.html',
name: 'home'
}).
when('/about', {
templateUrl: 'views/about.html',
name: 'about'
});
Than add $route to your main controller, and pass it on to $scope 比将$ route添加到您的主控制器,并将其传递给$ scope
function MainCtrl($scope, $route) {
$scope.$route = $route;
}
Now you can add the active class using ng-class 现在您可以使用ng-class添加活动类
<a ng-class="{active: $route.current.name == 'home'}"> Home </a>
<a ng-class="{active: $route.current.name == 'about'}"> About </a>
demo: https://jsfiddle.net/jkrielaars/mao8kx0L/2/ 演示: https : //jsfiddle.net/jkrielaars/mao8kx0L/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.