我正在添加过滤器以显示类别,但我的setCurrentCategory函数未显示任何值显示未定义。我想为每个人展示类别。我使用ng-click并传递my函数,而不是使用filter并传递值。
var myapp = angular.module("myapp", []);
myapp.controller("myCtrl", function($scope){
$scope.categories = [
{'id' : 0, 'Name' : 'Manu'},
{'id' : 1, 'Name' : 'Rajveer'},
{'id' : 2, 'Name' : 'Heament'},
{'id' : 3, 'Name' : 'Yogesh'},
{'id' : 5, 'Name' : 'Sajid'},
];
$scope.languages = [
{'id' : 0, 'title' : 'Angular', 'url' : 'http://angularjs.org', "category": "Manu"},
{'id' : 0, 'title' : 'Html5', 'url' : 'http://www.html5rocks.com/en/', "category": "Manu"},
{'id' : 0, 'title' : 'BootStrap', 'url' : 'http://getbootstrap.com/', "category": "Rajveer"},
{'id' : 0, 'title' : 'Css', 'url' : 'http://www.tutorialspoint.com/css/', "category": "Yogesh"},
{'id' : 0, 'title' : 'emberjs', 'url' : 'http://emberjs.com/', "category": "Heament"},
{'id' : 0, 'title' : 'Javascript', 'url' : 'https://www.javascript.com/', "category": "Rajveer"},
{'id' : 0, 'title' : 'jQuery', 'url' : 'https://jquery.com/', "category": "Yogesh"},
{'id' : 0, 'title' : 'Photoshop', 'url' : 'http://www.photoshop.com/', "category": "Sajid"},
{'id' : 0, 'title' : 'Coral Draw', 'url' : 'http://www.coreldraw.com/in/', "category": "Heament"},
{'id' : 0, 'title' : 'Flash', 'url' : 'https://get.adobe.com/flashplayer/', "category": "Sajid"},
];
$scope.currentCategory = null;
function setCurrentCategory(category){
$scope.currentCategory = category;
}
function isCurrentCategory(category){
return $scope.currentCategory !== null && category.name === $scope.currentCategory.name;
}
$scope.setCurrentCategory = setCurrentCategory;
$scope.isCurrentCategory = isCurrentCategory;
});
.left_box{background-color: #909090; padding: 0px;}
.right_box{padding-top: 25px;}
.nav__box{
padding-top: 25px;
}
.nav__box ul{ padding: 0px; margin: 0px; }
.nav__box ul li {list-style: none; padding: 10px; text-align: center;}
.nav__box ul li a{
font-size: 16px;
color: black;
text-decoration: none;
}
.nav__box ul li a:hover{background: }
.language_list { padding: 0px; margin: 0px; }
.language_list li{list-style: none; padding: 5px 10px; margin-bottom: 2px; border: solid 1px #ededed}
.language_list li a {text-decoration: none; padding-left: 15px}
.remove{float: right; font-size: 12px; color: red; line-height: 16px;}
.edit{font-size: 12px; color: red; line-height: 16px; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" class="container-fluid" ng-controller="myCtrl">
<div class="row">
<article class="col-md-1 left_box">
<a href="" ng-click="setCurrentCategory(null)"><img src="images/logo.png" alt="AngularJs"/></a>
<nav class="nav__box">
<ul>
<li ng-repeat='categorie in categories'>
<a href="#" ng-click="setCurrentCategory(category)">{{categorie.Name}}</a>
</li>
</ul>
</nav>
</article>
<article class="col-md-11 right_box">
<ul class="language_list">
<li ng-repeat= "language in languages | filter:{category:currentCategory.Name}">
<span>Edit</span>
<a href='{{language.url}}' target="_blank">{{language.title}}</a>
<span class="remove">Close</span>
</li>
</ul>
</article>
</div>
</div>
答案 0 :(得分:0)
确保您传递category
对象:
<li ng-repeat='category in categories'> // changed to 'category'
<a href="#" ng-click="setCurrentCategory(category)">{{category.Name}}</a>
</li>
答案 1 :(得分:0)
<ul>
<li ng-repeat='categorie in categories'>
<a href="#" ng-click="setCurrentCategory(category)">{{categorie.Name}} </a>
</li>
</ul>
我需要传递类别
<ul>
<li ng-repeat='categorie in categories'>
<a href="#" ng-click="setCurrentCategory(categorie )">{{categorie.Name}}</a>
</li>
</ul>
答案 2 :(得分:0)
在您的代码中,您必须使用ng-repeat中使用的相同引用传递给Setcurrentcategory函数。他们必须匹配。
<li ng-repeat='category in categories'>
<a href="#" ng-click="setCurrentCategory(category)">{{category.Name}}</a>
</li>