'use strict';
app.factory('addressService', ['$http', '$q', 'ngAuthSettings',
function($http, $q, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
var addressServiceFactory = {};
var _createAddress = function(address) {
var mydata = {
title: 'foo',
body: 'bar',
userId: 1
}
var deferred = $q.defer();
$http.post('http://jsonplaceholder.typicode.com/posts', mydata, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(response) {
alert("success");
deferred.resolve(response);
}).error(function(err, status) {
alert("fail");
deferred.reject(err);
});
return deferred.promise;
};
addressServiceFactory.getAddresses = _getAddresses;
addressServiceFactory.createAddress = _createAddress;
return addressServiceFactory;
}]);
app.controller('createAddressController', ['$scope', 'addressService',
'attributeService', function($scope, $addressService, $attributeService) {
$scope.createAddress = function() {
$addressService.createAddress($scope.address)
.then(function(results) {
alert("commited");
$state.go('addresses');
//$scope.addresses = results;
}, function(error) {
alert("Oops there has been an error saving the address");
});
};
}]);
ive cleaned the code up above..(easier to read)
On Wednesday, March 2, 2016 at 4:00:22 PM UTC, Neil Hewitt wrote:
>
> Hi Sander thanks for the help. This code is contained within a factory
> which is why i was using the defer (though i may still be wrong doing
> so).The idea being that as i cannot wait for the response i have to pass
> the promise back to the controller and continue in the .then of the
> original call (still getting used to async).
> Ive included my full factory and controller below and have hooked up to a
> opensource API just to rule my API out. Now i'm starting to think there may
> be something going on network wise my end as this only works
> intermittently. When it works i can see (via fiddler) that data is passed
> to the service (Json Object) see below 1 success 2 fails.
>
> <https://lh3.googleusercontent.com/-TbAchqjdeo4/VtcNg7M_MdI/AAAAAAAALP4/WfoLmL5BAL8/s1600/Snap%2B2016-03-02%2Bat%2B15.57.40.png>
>
>
>
>
>
>
>
>
>
>
> When it does not work no data is passed.. its really strange. Im very
> intrigued by the sample you provided and the potential to remove the
> defer/. Please would you be able to demonstrate a call from a controller
> calling a service and handling error/success ?
>
> 'use strict';
> app.factory('addressService', ['$http', '$q', 'ngAuthSettings',
> function ($http,$q, ngAuthSettings) {
>
> var serviceBase = ngAuthSettings.apiServiceBaseUri;
> var addressServiceFactory = {};
> var _createAddress = function (address) {
> var mydata = {
> title: 'foo',
> body: 'bar',
> userId: 1
> }
>
> var deferred = $q.defer();
>
> $http.post('http://jsonplaceholder.typicode.com/posts', mydata, {
> headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
> }).success(function (response) {
> alert("success");
> deferred.resolve(response);
> }).error(function (err, status) {
> alert("fail");
> deferred.reject(err);
> });
> return deferred.promise;
> };
>
> addressServiceFactory.getAddresses = _getAddresses;
> addressServiceFactory.createAddress = _createAddress;
> return addressServiceFactory;
> }]);
>
> app.controller('createAddressController',
> ['$scope','addressService','attributeService', function
> ($scope,$addressService,$attributeService) {
> $scope.createAddress = function (){
> $addressService.createAddress($scope.address)
> .then(function (results) {
> alert("commited");
> $state.go('addresses');
> //$scope.addresses = results;
> }, function(error){
> alert("Oops there has been an error saving the address");
> });
> };
> }]);
>
> Thanks again for the assistance.
>
>
>
> On Wednesday, March 2, 2016 at 12:49:35 PM UTC, Sander Elias wrote:
>>
>> Hi Neil,
>>
>> First of all, you should drop the entirely unneeded defer.
>> This will do exactly the same:
>>
>> var _createAddress = function (address) {
>> return $http.post(serviceBase + 'api/Addresses/Address',address)
>> .then(function (response) {return response.data}); // strip out
>> the data from the response, wich is the only difference between .success
>> and .then
>> };
>>
>> Do I read it correctly that you are debugging your server? If this is the
>> case, setting the breakpoint might be the cause of the err:null.
>>
>> Regards
>> Sander
>>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.