【发布时间】:2015-07-14 13:41:14
【问题描述】:
我有一些数据存储在 nosql 数据库中,当我访问 localhost:3000/countries 时可以看到这些数据。
我正在尝试使用这个工厂来获取 json 中的数据。
.factory('countries', ['$http', '$q',
function($http, $q) {
var countries = {
get: function() {
return $q(function(resolve, reject) {
$http({
method: 'GET',
url: '/countries'
}).then(function(response) {
resolve(response.data);
}, function(error) {
reject(error);
});
});
};
return countries;
}
]);
然后我尝试使用此控制器将该数据发送到要显示的适当 url,但它没有像我预期的那样工作。
.controller('CountriesCtrl', function($scope, countries) {
console.log(countries);
countries.get(function(countries) {
$scope.countries = countries;
});
})
有什么想法或建议吗?谢谢!
【问题讨论】:
-
你需要注入你新创建的工厂服务,即devices
-
如果您使用ui-router resolve 为您的控制器提供该州自定义的内容或数据。在解析函数中使用@Alex 描述的服务,然后在控制器内部,您只需注入解析的名称,数据就可以使用了。
标签: json angularjs get http-request