我想在選中一個或多個複選框時顯示帶有選項的div,但我似乎無法弄清楚ng-show=""
代碼應該是什麼。什麼是這個jQuery代碼的AngularJS等價物?AngularJS相當於jQuery:檢查代碼
$('.checkboxes:checked').length > 0
我想在選中一個或多個複選框時顯示帶有選項的div,但我似乎無法弄清楚ng-show=""
代碼應該是什麼。什麼是這個jQuery代碼的AngularJS等價物?AngularJS相當於jQuery:檢查代碼
$('.checkboxes:checked').length > 0
,我發現我的解決方案:
對於複選框,中號使用:
<div ng-repeat="item in items">
<input type="checkbox" ng-model='item.selected' />
</div>
而且ng-show=""
: ng-show="(items|filter:{selected:true}).length"
謝謝您的回答!
如這裏指出:https://docs.angularjs.org/api/ng/directive/ngShow
如果表達式爲truthy則該元素被顯示或隱藏分別 。
所以,如果你正在尋找類似
<input type="checkbox" ng-model="boolExpr" />
<div ng-show="boolExpr"></div>
不,優雅,但也許你可以使用這個:
<!doctype html>
<html ng-app="app">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script>
var app = angular.module('app', []);
app.controller('MyCtrl', function ($scope) {
$scope.options = [
{'title' : 'Title1', 'value' : 'A'},
{'title' : 'Title2', 'value' : 'B'},
{'title' : 'Title3', 'value' : 'C'}
];
$scope.checkboxes = {};
$scope.showMessage = function() {
for (var key in $scope.checkboxes) {
if ($scope.checkboxes[key] === true) return true;
}
return false;
}
});
</script>
</head>
<body>
<div ng-controller="MyCtrl">
<label ng-repeat="option in options">
<input
type="checkbox"
value="{{option.value}}"
ng-model="checkboxes[option.value]"
> {{option.title}}
</label>
Options: {{checkboxes|json}}<br>
Show message: {{showMessage()|json}}<br>
<div ng-show="showMessage()">
hey
</div>
</div>
</body>
</html>