Script.js
var app=angular.module("myapp",["panelModule"]);
var store=this;
store.products=[];
//following code yield proper output
// app.controller("StoreController",function () {
// this.products=data; //data is a array of object defined in same file
// });
app.controller("StoreController",['$http',function($http){
return $http.get('js/data.json').then(onDataReceive);
}]);
var onDataReceive=function(response) {
store.products.push(...response.data);
console.log(store.products); //this shows [object, object] in browser console
};
मैं मूल रूप से अपने विचार (index.html) में एनजी-रिपीट के साथ ऑब्जेक्ट की सरणी पर पुनरावृति कर रहा हूं, लेकिन जब मैंने $ http सेवा का उपयोग किया तो यह अपडेट नहीं हो रहा है। हालांकि स्थिर डेटा ठीक से प्रदर्शित हो रहा है। मैं कुछ ऑनलाइन AngularJs ट्यूटोरियल के माध्यम से जा रहा हूँ। मैं AngularJs के लिए नया हूँ। कृपया मुझे बताएं कि मैं यहाँ क्या गलत कर रहा हूँ? धन्यवाद
0
smasher
28 फरवरी 2017, 16:38
2 जवाब
सबसे बढ़िया उत्तर
var app = angular.module("myapp", ["panelModule"]);
app.controller("StoreController", ['$http', function($http) {
var store = this;
store.products = [];
gettingAPIData();
function gettingAPIData() {
$http.get('js/data.json').then(function(response) {
store.products. = response.data;
onDataReceive();
});
}
function onDataReceive() {
console.log(store.products);
};
}]);
इसे करने का यह तरीका है, आप पहले एक मेथड बनाएं और उसमें http स्टफ करें। सफलता प्रतिक्रिया में किसी अन्य विधि को कॉल करें और उसका प्रिंट आउट लें।
1
Rakeschand
28 फरवरी 2017, 19:00
इसे आजमाएं:
var app = angular.module("myapp", ["panelModule"]);
app.controller("StoreController", ['$http', function($http) {
var store = this;
store.products = [];
getData = function() {
return $http.get('js/data.json').then(function(response) {
$scope.data = response.data;
});
}
// do the ajax call
getData().then(function(data) {
// stuff is now in our scope, I can alert it
store.products = $scope.data;
console.log(store.products);
});
2
Rohit Jindal
1 मार्च 2017, 13:16