2016-07-28 150 views
0

我有嵌套的切換按鈕,默認情況下它是關閉的,當它將值保存到localstorage時。當一個人打開了我想做的事就是和你試圖打開其他上,第一個應該被自動關閉,而在當另一個打開時自動關閉切換按鈕

.controller('shops',['$scope','$http','$timeout',function($scope,$http,$timeout){ 
$http.get('http://localhost/moves/templates/shopping.php').success(function(data){ 
     $scope.shops=data ; 
     }); 

$scope.pushNotification = {}; 
    $scope.pushNotification.text = "Sample" 
    $scope.pushNotification.checked = false; 

    $scope.pushNotificationChange = function(item) { 
    console.log('Push Notification Change', $scope.pushNotification.checked); 
     if($scope.pushNotification.checked){ 
      localStorage.setItem("shop_id",($scope.item.shop_id)); 
     }else{ 
      localStorage.removeItem("shop_id"); 
     } 
    }; 


    //$scope.pushNotification = { checked: false }; 

}]) 

HTML其他輪流

<div ng-controller="shops" ng-repeat="item in shops"> 
      <ion-item class="item-thumbnail-left item-text-wrap"> 
      <img src="img/index/fashion.png" alt="photo" width="32" height="32" /> 
      <h2>{{item.shop_name}} </h2> 
      <p>{{item.biz_location}}</p> 

    <input type="hidden" value="{{item.shop_id}}"> 

      <div align="right"> 
      <label class="toggle toggle-balanced"> 
      <input type="checkbox"ng-model="pushNotification.checked" 
        ng-change="pushNotificationChange()"> 
      <div class="track"><div class="handle"></div></div> 
      </label> 
      </div> 
      </ion-item> 
      </div> 
+0

只有一個複選框嗎? –

+0

@AaronSaunders,他們或兩個,可以更多 – user6579134

回答

0

代替您的複選框有一個複選框ng-model,您希望每個項目都有自己的ng-model與每個特定項目綁定。這樣,當一個人改變並擊中範圍上的單個更改方法時,您可以根據需要更新每個項目的複選框模型(在這種情況下,將選中狀態設置爲false)。

這裏的工作的例子,在OP簡化現有代碼:

angular.module('App', []) 
 
.controller('Shops',['$scope','$http','$timeout',function($scope,$http,$timeout){ 
 

 
    $scope.shops = [ 
 
    { 
 
     shop_id: 1, 
 
     shop_name: 'Shop One', 
 
     checked: false, 
 
    }, 
 
    { 
 
     shop_id: 2, 
 
     shop_name: 'Shop Two', 
 
     checked: false, 
 
    }, 
 
    { 
 
     shop_id: 3, 
 
     shop_name: 'Shop Three', 
 
     checked: false, 
 
    } 
 
    ]; 
 

 
    $scope.onItemChange = function(item) { 
 
    // Loop over all our shops and set other checked properties to false if not our current item 
 
    angular.forEach($scope.shops, function(shop) { 
 
     if (shop !== item) { 
 
     shop.checked = false; 
 
     } 
 
    }); 
 
    // Do whatever else when an item changes 
 
    }; 
 
}]);
<body ng-app="App"> 
 
    <div ng-controller="Shops"> 
 
    <div ng-repeat="item in shops"> 
 
     <!-- Each checkbox is using a specific model for each item --> 
 
     <input type="checkbox" ng-model="item.checked" ng-change="onItemChange(item)"> 
 
     <span>{{item.shop_name}}</span> 
 
    </div> 
 
    </div> 
 
    
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
 
</body>

+0

感謝您的答案,並對我遲到的答覆感到抱歉,我不得不關注一些事情。我很困惑,因爲我不知道如何把你的答案放到我的腳本中。我在想,如果你可以把它寫入我的腳本的問題 – user6579134

相關問題