我尝试读取远程js中的特定数据(可通过ajax调用获得),因此我在控制器中调用了一个工厂:
app.controller('ParamsCtrl', ['$scope', '$http', 'DistantFactory', function($scope, $http, DistantFactory) {
$scope.dataD = DistantFactory.getDistant();
}]);
我的工厂:
app.factory('DistantFactory', function($http, $q) {
var factory = {
distant : false,
getDistant : function() {
var deferred = $q.defer();
$http({method: 'GET', url: urlDataDistant, cache: false, timeout: 1500}).
success(function(data, status) {
/* define data */
factory.distant = paramsField.params[0].data.date;
deferred.resolve(factory.distant);
}).error(function(data, statuts) {
deferred.reject('ERROR');
});
return deferred.promise;
}
}
// return
return factory;
});
我有这个错误:
Cannot read property 'getDistant' of undefined
遥远的js:
var paramsField = {
"params" : [
{
"data" : {
"date" : "2014-05-03"
}
}
]
}