我在Angularjs中有一个导航,其中包含来自对象的ID的URL 现在我想让这些导航项设置为活动状态,如果它与当前位置匹配。
模板
<li ng-class="{active:isActive('/s/{{subject.id}}/popular')}"><a href="#/s/{{subject.id}}/popular">POPULAR</a></li>
<li ng-class="{active:isActive('/s/{{subject.id}}/latest')}"><a href="#/s/{{subject.id}}/latest">LATEST</a></li>
控制器
angular.module('pob.controllers', []).
controller('appController', function($scope, $location, $routeParams, APIservice){
$scope.isActive = function(url) {
console.log(url);
if (url == $location.path()) {
return true;
}
return false;
};
}.controller('subjectController', function($scope, $routeParams, APIservice) {
$scope.id = $routeParams.id;
$scope.page = $routeParams.page;
$scope.subject = [];
$scope.votes = [];
$scope.vote = function(vote, comment, id){
console.log(vote);
console.log(comment);
console.log(id);
};
APIservice.getSubject($scope.id, $scope.page).success(function (response) {
$scope.subject = response.subject;
$scope.votes = response.votes;
});
});
问题是“url”与该位置不匹配,因为console.log(url)返回“/ s // popular”。
我如何解决这个问题?