I have just started using angular js. I am using it with CodeIgniter. I am trying to add a {class="active"} if a condition matches.
I am trying to add a active class to a <li> if its title matches with the last segment of url.
i.e.
if(title of this nav_menu matches with the last uri segment)
add "active" class
else
do nothing
My code is as follows :
header.php
<ul class="nav navbar-nav" ng-app="" ng-controller="menuController">
<li ng-repeat="x in menus" ng-class="{'active': x.name = scope}">
<a ng-href="http://{{ x.link}}">{{ x.name }}</a>
</li>
</ul>
footer.php
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
function menuController($scope) {
$scope.menus = [ {
name : "Home",
link : "projects/test/home"
}, {
name : "Daily Special",
link : "projects/caesar/daily-especial"
}, {
name : "Menu",
link : "projects/test/menu"
}, {
name : "Special",
link : "projects/test/special"
}, {
name : "Gallery",
link : "projects/test/gallery"
}, {
name : "About us",
link : "projects/test/about-us"
}, {
name : "Contact",
link : "projects/test/contact"
} ];
$scope.current = '<?php echo $this->uri->segment(2); ?>';
}
</script>
I think the problematic step is $scope.current = 'uri->segment(2); ?>';
ng-class="{'active': x.name.toLowerCase().replace(/ /g, '-') == x.link.substr(link.lastIndexOf('/') + 1, link.length)}"?