Angular.js實現(xiàn)多個checkbox只能選擇一個的方法示例
首先來看看效果

效果
實現(xiàn)這樣的效果,必須使用指令了,只有使用指令才能單獨控制每一個scope。
示例代碼如下:
<div class="form-group">
<label class="col-sm-2 control-label">請選擇文章主題色彩</label>
<div class="col-sm-10" theme-group>
<label class="i-switch m-t-xs m-r">
<input type="checkbox" input-theme >
<i></i>
</label>
<label class="i-switch bg-info m-t-xs m-r">
<input type="checkbox" input-theme>
<i></i>
</label>
<label class="i-switch bg-primary m-t-xs m-r">
<input type="checkbox" input-theme >
<i></i>
</label>
<label class="i-switch bg-danger m-t-xs m-r">
<input type="checkbox" input-theme>
<i></i>
</label>
</div>
</div>
(function () {
angular
.module("shishuoproject")
.directive("themeGroup",function(){
return{
controller: function () {
var scopeArray=[];
this.addScope= function (scope) {
var self=this;
scopeArray.push(scope);
scope.$on("$destory",function(){
self.removeScope(scope);
})
};
this.closeScope= function (scope) {
//var l=scopeArray.length;
angular.forEach(scopeArray, function (value) {
if(value!=scope){
value.flag=false;
}
})
};
this.removeScope= function (scope) {
var index=scopeArray.indexOf(scope);
if(index!==-1){
scopeArray.splice(index,1);
}
};
this.getIndex= function (scope) {
var index=scopeArray.indexOf(scope);
return index;
}
}
}
})
.directive("inputTheme",function(){
return{
restrict:'EA',
require:"^?themeGroup",
template:'<input type="checkbox" ng-click="choseTheme()" ng-model="flag">',
replace:true,
scope:{},
link: function (scope,element,attr,themeCon) {
var colorArray=['#27c24c','#23b7e5','#7266ba',' #f05050'];
themeCon.addScope(scope);
scope.choseTheme= function () {
themeCon.closeScope(scope);
var index=themeCon.getIndex(scope);
var color=colorArray[index];
scope.$emit("getArticleThemeColor",{'color':color});
console.log(scope.flag);
};
}
}
})
})()
這里簡單說一下,實現(xiàn)的主要思想就是,通過指令單獨生成scope,每一個指令都是一個單獨的scope,這樣每個ng-modal都獨立出來了,然后通過繼承一個總的指令來實現(xiàn)控制。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者使用Angular.js能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
AngularJs的UI組件ui-Bootstrap之Tooltip和Popover
這篇文章主要介紹了AngularJs的UI組件ui-Bootstrap之Tooltip和Popover,tooltip和popover是輕量的、可擴(kuò)展的、用于提示的指令。具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Angular outlet實現(xiàn)頁面布局示例詳解
這篇文章主要為大家介紹了Angular outlet實現(xiàn)頁面布局示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Angular2生命周期鉤子函數(shù)的詳細(xì)介紹
這篇文章主要介紹了Angular2生命周期鉤子函數(shù)的詳細(xì)介紹,Angular提供組件生命周期鉤子,可以讓我們更好的開發(fā)Angular應(yīng)用,有興趣的可以了解一下2017-07-07
詳解Angularjs在控制器(controller.js)中使用過濾器($filter)格式化日期/時間實例
本篇文章主要介紹了詳解Angularjs在控制器(controller.js)中使用過濾器($filter)格式化日期/時間實例 ,有需要的小伙伴可以參考下。2017-02-02
angular內(nèi)置provider之$compileProvider詳解
下面小編就為大家?guī)硪黄猘ngular內(nèi)置provider之$compileProvider詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
AngularJS實用基礎(chǔ)知識_入門必備篇(推薦)
下面小編就為大家?guī)硪黄狝ngularJS實用基礎(chǔ)知識_入門必備篇(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
angular實現(xiàn)spa單頁面應(yīng)用實例
本篇文章主要介紹了angular實現(xiàn)spa單頁面應(yīng)用實例,小本篇文章是對單頁面的一個簡單的基本邏輯操作,這個方法可以搭建基本的單頁面的邏輯結(jié)構(gòu)。一起跟隨小編過來看看吧2017-07-07

