我怎样才能从工厂得到具体的结果?
How can I get specific results from a factory?
我只是在学习 angularjs 并且正在努力从这家工厂获得我想要的结果 :
app.factory('foreCast', ['$http', function($http,$scope ) {
var req = $http.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK");
req.success(function (data) {
req.rows =data.results.collection1;
req.rand =req.rows[Math.floor(Math.random() * req.rows.length)];
console.log(req.rand);
//i want this results
});
return req; //how can i return req.rand?
}]);
app.factory('foreCast', ['$q', '$http', function ($q, $http) {
return {
getRand: function () {
var deferred = $q.defer();
$http
.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK")
.then(function (response) {
var rows = response.data.results.collection1;
var rand = rows[Math.floor(Math.random() * rows.length)];
deferred.resolve(rand);
}, function (err) {
deferred.reject(err);
})
return deferred.promise;
}
}
}])
然后从控制器中像这样使用它:
foreCast.getRand().then(function(rand){
var myRand = rand;
})
所以首先,您永远不想将成功处理程序与 http 一起使用。我相信它实际上已被弃用。最佳实践是对错误使用 .then 和 .catch。其次,大多数时候您应该在控制器中处理结果,因为服务不应该知道如何处理数据。
话虽这么说,我们可以 trim 稍微降低您的工厂。
app.factory('foreCast', ['$http', function($http,$scope ) {
var factory = {};
factory.rand = function() {
return $http.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK");
};
return factory;
}]);
现在在您的控制器中:
app.controller('controller', ['foreCast', function(foreCast) {
var self = this;
foreCast.rand().then(function(response) {
var rows = response.results.collection1;
self.rand = rows[Math.floor(Math.random() * rows.length)];
});
}]);
现在在您看来,只需使用 rand 访问此值即可。
<div ng-bind="ctrl.rand"></div>
我只是在学习 angularjs 并且正在努力从这家工厂获得我想要的结果 :
app.factory('foreCast', ['$http', function($http,$scope ) {
var req = $http.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK");
req.success(function (data) {
req.rows =data.results.collection1;
req.rand =req.rows[Math.floor(Math.random() * req.rows.length)];
console.log(req.rand);
//i want this results
});
return req; //how can i return req.rand?
}]);
app.factory('foreCast', ['$q', '$http', function ($q, $http) {
return {
getRand: function () {
var deferred = $q.defer();
$http
.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK")
.then(function (response) {
var rows = response.data.results.collection1;
var rand = rows[Math.floor(Math.random() * rows.length)];
deferred.resolve(rand);
}, function (err) {
deferred.reject(err);
})
return deferred.promise;
}
}
}])
然后从控制器中像这样使用它:
foreCast.getRand().then(function(rand){
var myRand = rand;
})
所以首先,您永远不想将成功处理程序与 http 一起使用。我相信它实际上已被弃用。最佳实践是对错误使用 .then 和 .catch。其次,大多数时候您应该在控制器中处理结果,因为服务不应该知道如何处理数据。
话虽这么说,我们可以 trim 稍微降低您的工厂。
app.factory('foreCast', ['$http', function($http,$scope ) {
var factory = {};
factory.rand = function() {
return $http.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK");
};
return factory;
}]);
现在在您的控制器中:
app.controller('controller', ['foreCast', function(foreCast) {
var self = this;
foreCast.rand().then(function(response) {
var rows = response.results.collection1;
self.rand = rows[Math.floor(Math.random() * rows.length)];
});
}]);
现在在您看来,只需使用 rand 访问此值即可。
<div ng-bind="ctrl.rand"></div>