2016-04-10 19 views
4

iyon onay kutusunun değerini nasıl:Bu kodu kullanarak benim onay kutusu listesini görüntülemek

$scope.listPref = [ 
    {text: 'Cinema'}, 
    {text: 'Sport'}, 
    {text: 'It'} , 
    {text: 'Music'}, 
    {text: 'Theater'}, 
    {text: 'Conference'}]; 

Ben seçilen tüm metnini almak için bu kodu deneyin: Bu benim listPref olduğunu

<ion-checkbox ng-repeat="item in listPref" 
    ng-model="item.checked" ng-checked="item.checked"> 
    {{ item.text }} 
</ion-checkbox> 

for(var i =0 ; i <$scope.listPref.length ; i++){ 
console.log($scope.listPref[i].checked.text); 
} 

madde

benim konsolunda mesaj undefined Cannot read property 'text' of undefined at Scope.$scope.pref olsun. Birisi bana yardım edebilir.

cevap

5

HTML: (Kontrolörün içinde)

<ion-content> 
    <ion-checkbox ng-repeat="item in listPref" 
     ng-model="checkItems[item.text]" ng-change="print()"> 
     {{ item.text }} 
    </ion-checkbox> 
    <button type="button" name="button" ng-click="save()">Save</button> 
</ion-content> 

JS: Eğer değerlerle istediğini Sen yapabilirsin

$scope.listPref = [ 
    {text: 'Cinema'}, 
    {text: 'Sport'}, 
    {text: 'It'} , 
    {text: 'Music'}, 
    {text: 'Theater'}, 
    {text: 'Conference'} 
]; 

$scope.checkItems = { } 

$scope.print = function() { 
    console.log($scope.checkItems); 
} 

$scope.save = function() { 
    var array = []; 
    for(i in $scope.checkItems) { 
     console.log($scope.checkItems[i]); 
     if($scope.checkItems[i] == true) { 
      array.push(i); 
     } 
    } 
    console.log(array); 
} 

. print() sadece kontrol edilen veya işaretlenmemiş olan tüm değerlerin nesnesini (doğru/yanlış) yazdırır. save() işaretli onay kutusunun değerini bir dizinin içine kaydeder ve ardından diziyi yazdırır. Değerleri ile neyi istersen yapabilirsin, bu örnek onları bir dizide saklar ve konsola yazdırır, böylece neler olup bittiğini kolayca görebilirsin.

Herhangi bir sorunuz varsa yorum yapın.

+0

thnkx, mükemmel çalışır. Ancak veritabanımda saklamak için sadece metin değeri alırdım, ben de 'console.log'u ($ scope.checkItems [i] .text),' 'undefined 'hatasını alıyorum. Cevabın için teşekkür ederim. –

+0

Veritabanına ne depolıyorsunuz ve nasıl saklıyorsunuz? Değere göre değer, dizi, nesne, vb? – theblindprophet

+0

Ayrıca, checkItems, anahtarın onay kutusundaki metnin olduğu bir nesnedir. {Sinema: doğru, Spor: yanlış}. Referans vermeye çalıştığınız "metin" adı verilen bir özellik yok. – theblindprophet