考慮下面的代碼:我的任務是檢查是否有4個或更多這些按鈕被選中,如果是,我需要顯示一個類似「你只能選擇3個過濾器「。請注意,我需要在啓動研究的按鈕之前執行此功能,所以我需要Angular在用戶激活和停用按鈕時不斷檢查這些布爾值。我嘗試使用$ watch和ng-change,但它們沒有按預期工作。3個或更多布爾變成真時執行函數angularJs
我該怎麼做?希望我提醒自己,事先感謝,隨時詢問你是否需要澄清。
HTML和控制器邏輯在這裏:
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn"
style="text-align: left; width: 100%;"
ng-click="smsFilter =! smsFilter"
ng-class="{'active': smsFilter == true}">Sms</button>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="videoChatFilter =! videoChatFilter"
ng-class="{'active': videoChatFilter == true}">Video Chat</button>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="chatFilter =! chatFilter"
ng-class="{'active': chatFilter == true}">Chat</button>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn"
style="text-align: left; width: 100%;"
ng-click="socialFilter =! socialFilter"
ng-class="{'active': socialFilter == true}">Social</button>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="emailFilter =! emailFilter"
ng-class="{'active': emailFilter == true}">Email</button>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="telefonateFilter =! telefonateFilter"
ng-class="{'active': telefonateFilter == true}">Telefonate</button>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn"
style="text-align: left; width: 100%;"
ng-click="formTicketFilter =! formTicketFilter"
ng-class="{'active': formTicketFilter == true}">Form Ticket</button>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="chatOfferFilter =! chatOfferFilter"
ng-class="{'active': chatOfferFilter == true}">Chat Offer</button>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="bannerFilter =! bannerFilter"
ng-class="{'active': bannerFilter == true}">Banner</button>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn"
style="text-align: left; width: 100%;"
ng-click="webFilter =! webFilter"
ng-class="{'active': webFilter == true}">Web</button>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="agenziaFilter =! agenziaFilter"
ng-class="{'active': agenziaFilter == true}">Agenzia</button>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 col-lg-6">
<button class="btn btn-default filterBtn "
style="text-align: left; width: 100%;"
ng-click="crcFilter =! crcFilter"
ng-class="{'active': crcFilter == true}">CRC</button>
</div>
</div>
<div class="row" style="text-align: center;">
<button class="btn btn-default headerBtn" style="margin-top: 10px;" ng-click="applyFilters()">Apply Filters</button>
</div>
角控制器:
//FILTERS
$scope.smsFilter = false;
$scope.videoChatFilter = false;
$scope.chatFilter = false;
$scope.socialFilter = false;
$scope.emailFilter = false;
$scope.telefonateFilter = false;
$scope.formTicketFilter = false;
$scope.chatOfferFilter = false;
$scope.webFilter = false;
$scope.agenziaFilter = false;
$scope.crcFilter = false;
//function that executes the research
$scope.applyFilters = function(){...}
這正是我需要的,非常感謝! – steiacopi