最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular.js實現(xiàn)多個checkbox只能選擇一個的方法示例

 更新時間:2017年02月24日 11:47:18   作者:MakingChoice  
這篇文章主要給大家介紹了利用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)控制。

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者使用Angular.js能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

最新評論

辽宁省| 涿鹿县| 合阳县| 诸暨市| 南开区| 宜黄县| 镶黄旗| 格尔木市| 灵璧县| 天长市| 巴彦县| 体育| 类乌齐县| 蒲江县| 高要市| 西宁市| 松原市| 文安县| 沈丘县| 新津县| 扎赉特旗| 虞城县| 广水市| 富锦市| 常熟市| 皮山县| 英德市| 化德县| 瑞昌市| 乌兰县| 招远市| 肃宁县| 库伦旗| 万州区| 石柱| 黄山市| 昌邑市| 池州市| 城固县| 吐鲁番市| 安顺市|