javascript - not able to pull data in factory by online json files -
i created factory named service.js
myappservices=angular.module('myappservices',['ngresource']); myappservices.factory('profiledata',['$resource', function($resource){ return $resource('http://www.w3schools.com/json/mytutorials.txt', {}, { query: {method: 'get', isarray: false} }); }]);
and controller is:
myappcontrollers.controller('profilelistctrl',['$scope', 'profiledata', '$timeout', function($scope, profiledata, $timeout) { var promise = profiledata.query(); promise.$promise.then(function(response) { $scope.data= response; console.log($scope.data); }); }]);
i not getting console here because of unable fetch data. please me here.
- note
: if using saved file address instead of online url of json file working.
controller.js
(function () { angular .module('app') .controller('profilelistctrl', profilelistctrl); profilelistctrl.$inject = ['$scope', 'profiledatafactory']; function profilelistctrl($scope, profiledatafactory) { $scope.data = []; function init() { profiledatafactory.get().then(function (response) { angular.copy(response, $scope.data); console.log(response); }); } init(); } }());
factory.js
(function () { angular .module('app') .factory('profiledatafactory', profiledatafactory); profiledatafactory.$inject = ['$http']; function profiledatafactory($http) { var self = this; self.get = $http.get('http://www.w3schools.com/json/mytutorials.txt').then(function (response) { return response.data; }); return self; } }());
Comments
Post a Comment