1
我想要一個按鈕組充當單選按鈕,如here。Angular + ui.bootstrap.buttons +收音機過濾器
我要的是:
- 按鈕默認情況下應禁用,但所有條目應該默認顯示。
- 單選按鈕行爲。
我怎樣才能實現這與我目前的代碼?
我的HTML:
Filter By Title:
<div class="btn-group">
<button type="button" ng-repeat="title in titles" class="btn btn-default" ng-model="selected[title]" btn-checkbox>{{title}}</button>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="entry in entries | filter:byTitle">
<td>{{entry.title}}</td>
<td>{{entry.text}}</td>
</tr>
</tbody>
</table>
我的JS:
App.controller('MainCtrl', function($scope, $http) {
$http.get('output/entries.json')
.then(function(res){
$scope.entries = res.data;
});
$scope.selected = {};
$scope.titles = ["sometitle","someothertitle","anothertitle"];
function isChecked(obj){
var checked = [];
for(var key in obj){
if(obj[key])
checked.push(key);
}
return checked;
};
$scope.byTitle = function(entry){
var checked = isChecked($scope.selected);
return checked.indexOf(entry.title) > -1;
};
});
你會如何改變它以模擬真正的複選框行爲(多選)? – deekay
這應該是一個新問題imo,當你問單選按鈕,現在你想要它的複選框。無論如何,要做到這一點,你需要三個布爾變量 - 每個按鈕一個,只需要做到這一點,因此ng-click切換該變量。然後他們將獨立切換。 –