Angular.js實(shí)現(xiàn)多個(gè)checkbox只能選擇一個(gè)的方法示例
首先來看看效果
效果
實(shí)現(xiàn)這樣的效果,必須使用指令了,只有使用指令才能單獨(dú)控制每一個(gè)scope。
示例代碼如下:
<div class="form-group"> <label class="col-sm-2 control-label">請(qǐng)選擇文章主題色彩</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); }; } } }) })()
這里簡(jiǎn)單說一下,實(shí)現(xiàn)的主要思想就是,通過指令單獨(dú)生成scope,每一個(gè)指令都是一個(gè)單獨(dú)的scope,這樣每個(gè)ng-modal都獨(dú)立出來了,然后通過繼承一個(gè)總的指令來實(shí)現(xiàn)控制。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者使用Angular.js能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼
這篇文章主要為大家介紹了Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03AngularJs的UI組件ui-Bootstrap之Tooltip和Popover
這篇文章主要介紹了AngularJs的UI組件ui-Bootstrap之Tooltip和Popover,tooltip和popover是輕量的、可擴(kuò)展的、用于提示的指令。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Angular outlet實(shí)現(xiàn)頁(yè)面布局示例詳解
這篇文章主要為大家介紹了Angular outlet實(shí)現(xiàn)頁(yè)面布局示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Angular2生命周期鉤子函數(shù)的詳細(xì)介紹
這篇文章主要介紹了Angular2生命周期鉤子函數(shù)的詳細(xì)介紹,Angular提供組件生命周期鉤子,可以讓我們更好的開發(fā)Angular應(yīng)用,有興趣的可以了解一下2017-07-07詳解Angularjs在控制器(controller.js)中使用過濾器($filter)格式化日期/時(shí)間實(shí)例
本篇文章主要介紹了詳解Angularjs在控制器(controller.js)中使用過濾器($filter)格式化日期/時(shí)間實(shí)例 ,有需要的小伙伴可以參考下。2017-02-02angular內(nèi)置provider之$compileProvider詳解
下面小編就為大家?guī)硪黄猘ngular內(nèi)置provider之$compileProvider詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09AngularJS實(shí)用基礎(chǔ)知識(shí)_入門必備篇(推薦)
下面小編就為大家?guī)硪黄狝ngularJS實(shí)用基礎(chǔ)知識(shí)_入門必備篇(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07angular實(shí)現(xiàn)spa單頁(yè)面應(yīng)用實(shí)例
本篇文章主要介紹了angular實(shí)現(xiàn)spa單頁(yè)面應(yīng)用實(shí)例,小本篇文章是對(duì)單頁(yè)面的一個(gè)簡(jiǎn)單的基本邏輯操作,這個(gè)方法可以搭建基本的單頁(yè)面的邏輯結(jié)構(gòu)。一起跟隨小編過來看看吧2017-07-07