@Sidharth Panwar的答案多半是正確的
,但我不知道是否需要在<span ng-class="selected: selected[$index] ? 'selectedData' : ''"
selected:
,我設法使其與剛剛特雷納裏運營商的工作:
// Code goes here
angular.module("app",[])
.controller("controller", function($scope){
$scope.selected=[]; // <--- this is the most important part of the solution
$scope.clearSelector = function(){
for(var i = 0; i < $scope.selected.length; i++){
$scope.selected[i] = false;
}
};
});
.highlight{
color:red;
}
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="[email protected]*" data-semver="1.5.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
</head>
<body ng-controller="controller">
<ul ng-init="temp=[1,2,3]">
<li ng-repeat="i in temp" ng-click="selected[$index] = !selected[$index]" ng-class="selected[$index]?'highlight':''">
toggel between black and red
</li>
</ul>
<button ng-click="clearSelector()">clear</button>
</body>
</html>
如果您關注的是ng-repeat
有它自己的範圍,你不能操縱從父範圍的值(其中「清除」按鈕),創建父範圍的數組可以幫助你集中你的控制變量:
// both in child scope
selected=1; // create a new variable if no existing variable named
//"selected" in the current scope,
// this happens when javascript knows how to create stuff ie:primitives
selected[0]=1; // need to know the address of "selected", if we can't
// find it in the current scope, look it up in the parent scope
爲更多細節看看這個鏈接關於inheritance and scope
你是什麼意思清除過濾器? –
調用點擊函數是不正確的:'ng-click =「selected =!selected; ng-click =」filterData()「' – Vineet
我認爲你試圖在點擊按鈕時重置fiter,現在你是使用單擊列表項並使用ng-click =「selected =!selected;」來應用fiter。因此,您可以將相同的ng-click條件應用於按鈕。 – Navaneet