2012-12-13 2 views
5

का उपयोग कर बाहरी फ़ाइल से जेसन डेटा का उपयोग कैसे करूं मैं एक एप्लीकेशन विकसित कर रहा हूं। उस में मैंमैं कोणीय जेएस आराम सेवा

बाहरी फ़ाइल $ http.get() विधि का उपयोग कर जेसन डेटा पुनर्प्राप्त कर रहा हूं, यह ठीक काम करता है। अब मैं कोणीय

आराम सेवाओं का उपयोग करने की कोशिश कर रहा हूं। यह फ़िल्टर में ठीक काम कर रहा है, लेकिन जब मैं इसे नियंत्रक में उपयोग करता हूं तो यह

अपरिभाषित प्रदर्शित करता है।

//Service.js File 
angular.module('calenderServices', ['ngResource']). 
factory('Events', function($resource){ 
return $resource('/EventCalender/:File.json', {}, { 
query: {method:'GET', params:{File:'events'}, isArray:true} 
}); 
}); 


     //This is my app Module 
angular.module('calender', ['eventFilters','highlight','event','calenderServices']). 
config(['$routeProvider', function($routeProvider) { 
$routeProvider. 
    when('', {templateUrl: 'template.html', controller: MonthCtrl}). 
    otherwise({redirectTo: '/invalid'}); 
}]); 


    //This is my filter.js 
angular.module('eventFilters', ['calenderServices']).filter('compare', function(Events) { 
var events=Events.query(); 
alert(events[0].name); //it is displaying the name "Milestone1" }); 


    //This is my controller. 
function MonthCtrl($scope, Events){ 
var events=Events.query(); 
    alert(events[0].name); //it is displaying undefined 
    } 

    //whenever i try to use the variable 'events' in controller, it is displaying undefined or null. But in filter it is working fine. 

उत्तर

1

निम्नलिखित अभ्यस्त काम क्योंकि ngResource एक अतुल्यकालिक http अनुरोध बनाता है।

var events=Events.query(); 
alert(events[0].name); // <--- here events is an empty array 

आमतौर पर तुम सब करने की जरूरत है निम्नलिखित और अपने डेटा आपके विचार

$scope.events = Events.query() 

यह एक तुल्यकालिक आपरेशन की तरह दिखता है में प्रस्तुत करने के लिए उपलब्ध हो जाएगा, लेकिन यह नहीं है। यह काम पर कोणीय के जेन है। आप details from the docs सीख सकते हैं।

आगे डेटा की प्रक्रिया करने के लिए, आप भी get विधि

Events.query(function(events){ 
    // here you have access to the first event's name 
    console.log(events[0].name); 
}); 
यहाँ

एक काम उदाहरण है करने के लिए एक सफलता कॉलबैक पारित गुजारें सकता: http://plnkr.co/edit/NDZ2JWjMUoUvtzEuRUho?p=preview

+0

मैं इस कोशिश की, यह काम नहीं कर रहा था। मैंने अभी '$ scope.events' नाम '$ scope.eve' नाम बदल दिया है, यह अब काम कर रहा है। बहुत बहुत धन्यवाद। –