2016-07-27 56 views
0

我想在切換開關處於關閉狀態時顯示警報,但發現它很困難。 我跟着這個教程,但我的不工作,因爲它應該切換切換時的警報

http://codepen.io/rossmartin/pen/iwuvC

JS

.controller('shops',['$scope','$http','$timeout',function($scope,$http,$timeout){ 
    $http.get('http://localhost/site/templates/shop.php').success(function(data){ 
    $scope.shops=data ; 
    $scope.pushNotificationChange = function() { 
    $timeout(function() { 
     alert('Push Notification Change: '+ $scope.pushNotification.checked); 
    }, 0); 
    }; 
    $scope.pushNotification = { checked: true }; 
    }); 
}]) 

HTML

<ion-view align-title="center"> 
    <ion-content overflow-scroll="false" has-bouncing="true" class=padding> 
    <ion-list> 
    <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> 
     <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> 
    </ion-list> 
    </ion-content overflow-scroll="false" has-bouncing="true">   
</ion-view> 

如何使這項工作任何幫助嗎?

+1

爲什麼所有的代碼被包裝'$ http.get'成功裏面,應該是外面$ http callback –

+0

@PankajParkar,謝謝你的提示。 假設有兩個或更多的切換開關,默認情況下一個是自動打開,另一個打開時我如何使第一個關閉 – neiza

回答

0

試試這個

<ion-view align-title="center"> 
    <ion-content overflow-scroll="false" has-bouncing="true" class=padding> 
    <ion-list> 
    <div ng-controller="shops"> 
    <div 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> 
     <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> 
    </div> 
    </ion-list> 
    </ion-content overflow-scroll="false" has-bouncing="true">   
</ion-view> 
1

你的代碼裏面所有的HTTP,它應該是外:

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

    $scope.pushNotificationChange = function() { 
    $timeout(function() { 
     alert('Push Notification Change: '+ $scope.pushNotification.checked); 
    }, 0); 
    }; 
    $scope.pushNotification = { checked: true };  
}]) 
+0

謝謝它的工作。假設有兩個或更多的切換開關,默認情況下一個自動打開,另一個打開時,我如何使第一個關閉 – neiza

+0

在你有警報的函數中,你可以做這樣的事情$ scope .pushNotificationSecond = {checked:false}; pushNotificationSecond是第二個開關=) –