私はAngularJSを使い始め、現在約束を見ています。だから私の古いコードは次のようになります。解決済みの約束をAngularJSのスコープに渡すにはどうすればよいですか?
app.controller('CustomerController', function ($scope Customers, $q) {
init();
function init() {
Customers.getCustomers()
.then(function (response) {
$scope.customers = response.data;
}, function (error) {
console.log(error);
});
}
});
app.factory('Customers', function ($http) {
return {
getCustomers: function() {
return $http.get('/api/customers');
}
};
});
それでは、私は約束を作るために私のinit関数でやったことは、このようなものです:
function init() {
var deferred = $q.defer();
Customers.getCustomers()
.then(function (response) {
deferred.resolve(response.data); // how to pass this into my scope?
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
あなたが見ることができるように、私は渡すことができないんだけどそれは私の範囲にある。私はここで何か間違っていますか?
を。 –