2015-05-22 29 views
5

AngularJS'de yeniyim. JSON dosya verilerini fabrikada bulunan bir değişkene yüklemek istiyorum.JSON verilerini bir değişkene depolayın

myApp.factory('quizFactory', function() { 
    var questions = [ 
     { 
      "setId":1, 
      "question":"What is the value of the 7 in the following number? 850,765", 
      "options": ["70", "7", "7000", "700"], 
      "answer": 3 
     }, 
     { 
      "setId":2, 
      "question":"Is 7 & 9 even or odd?", 
      "options": ["Even", "Odd", "Can't Say", "Dont know"], 
      "answer": 1 
     }, 
     { 
      "setId":3, 
      "question":"In the number 5,281,946 what is the value of the 3rd place?", 
      "options": ["100", "10,000", "1,000,000", "1,000"], 
      "answer": 0 
     }, 
     { 
      "setId":4, 
      "question":"Is 12 + 50 even or odd?", 
      "options": ["Even", "Odd", "Can't Say", "Dont know"], 
      "answer": 0 
     }, 
     { 
      "setId":5, 
      "question":"What does the 3 represent in the number below? 3051", 
      "options": ["3", "30", "300", "3000"], 
      "answer": 3 
     } 
    ]; 

    return { 
     getQuestion: function(id) { 
      if(id < questions.length) { 
       return questions[id]; 
      } else { 
       return false; 
      } 
     } 
    }; 
}); 

Yukarıdaki kod, app.js dosyasında depolanır ve JSON dosyam yukarıdakiyle aynıdır.

[ 


{ 
     "setId":1, 
     "question":"What is the value of the 7 in the following number? 850,765", 
     "options": ["70", "7", "7000", "700"], 
     "answer": 3 
    }, 
    { 
     "setId":2, 
     "question":"Is 7 & 9 even or odd?", 
     "options": ["Even", "Odd", "Can't Say", "Dont know"], 
     "answer": 1 
    }, 
    { 
     "setId":3, 
     "question":"In the number 5,281,946 what is the value of the 3rd place?", 
     "options": ["100", "10,000", "1,000,000", "1,000"], 
     "answer": 0 
    }, 
    { 
     "setId":4, 
     "question":"Is 12 + 50 even or odd?", 
     "options": ["Even", "Odd", "Can't Say", "Dont know"], 
     "answer": 0 
    }, 
    { 
     "setId":5, 
     "question":"What does the 3 represent in the number below? 3051", 
     "options": ["3", "30", "300", "3000"], 
     "answer": 3 
    } 
]; 

this question da denedim.

cevap

3

json dosyasını okumak için $http'u kullanabilirsiniz. Örneğin.

angular.module('yourmodule') 
.factory('jsonResourceService', ['$http', 
function($http) { 
    var jsonResourceService = {}; 

    jsonResourceService.load = function(callback) { 
    $http.get('path/data.json').success(function(data) { 
     callback(data); 
    }); 
    return jsonResourceService; 


} 
]); 

ve bu gibi denetleyicisinden diyoruz:

$http.get('someFile.json').success(function(data) {  
     questions = data; 
    });  
+0

JS dosyamı myApp.factory ('quizFactory', ['$ http', function ($ http) { \t $ http.get ('ques.json') ile değiştirdim. Başarı (işlev (veri)) { var sorular = veri; }); }); ama hala çalışmıyor json dosyasını yükleyemiyor –

+0

Çalışmalı, json dosyanız doğru yerde olmayabilir. İşte çalışma demo http://plnkr.co/edit/PpRvBhbZ9LHT8EQeUV2a?p=preview – sol4me

+0

app.factory için app.factory değiştirirseniz app.js dosyasında –

0

Böyle Fabrikanızı yeniden tanımlayabilirsiniz

 $scope.objectsArray = []; 

    jsonResourceService.load(function(data) { 
$scope.objectsArray; 
}); 
+0

nedenEach döngüsü için ihtiyaç var? bunun yerine verileri doğrudan dizi nesnesine kaydedebilirsiniz. – Hardy

+0

Şimdi benim durumumda düzenlenmiş, diziye eklemeden önce bazı ekstra işlemler yapıyordum. –

0

Ben fabrika değişkene JSON verilerini eklemek istiyorsanız Bir denetleyicideki bazı özel isteklerde, ne anlama geliyorsa, o zaman $http tercih edilebilirdir:

Bağımlılığın fabrikasını fabrikaya enjekte etmeniz gerektiğini ve evet, verilerin elle ayrıştırılması gerekli olmadığına dikkat edin, Angular otomatik olarak bunlara bakacaktır.

İlgili konular