无法通过在线 JSON 文件在工厂中提取数据
not able to pull data in factory by online json files
我创建了一个名为 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}
});
}]);
我的控制器是:
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);
});
}]);
由于无法获取数据,我在这里没有得到任何控制台。请在这里帮助我。
- 注意
:如果我使用保存的文件地址而不是 json 文件的在线 url,那么它可以工作。
控制器.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();
}
}());
工厂.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;
}
}());
相关文章:
- 在数据提取完成之前进行页面渲染
- Angularjs工厂注入错误
- 如何将参数传递到angularJs中的工厂
- 为什么只有工厂在棱角分明的网站上被提及
- AngularJS:$q.dedefe()不能由工厂方法共享
- 如何在Javascript中从字符串中提取某些单词
- RegEx JavaScript:数字后的符号提取
- 如何在内联依赖项并将图像转换为dataURI的情况下完全提取网页
- Angular,函数在(模型)工厂中返回值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何使用异步调用更改工厂的变量
- Regex提取URL返回数组的一部分;未定义”;
- 将变量传递到工厂
- 如何提取“;href"最近列表项中的属性值
- 使用时刻.js从日期时间中提取时间
- 通过javascript从会话中提取用户名
- HTML页面如何提取通过表单传递的参数
- 是否可以自动拉取“随机”;标签“;从一长串文本中提取
- 无法通过在线 JSON 文件在工厂中提取数据
- 无法从工厂 AngularJS 中的 JSON 文件中提取数据