AngularJS $http is not defined - angularjs

I'm pretty new to with AngularJS. When I'm calling $http.get I get a $http is not defined error.
This is the content of my Module:
var demoApp = angular.module('demoApp', []);
demoApp.config(function ($routeProvider) {
$routeProvider.
when('/view1',
{
controller: 'SimpleController',
templateUrl: 'View1.html'
}).
when('/view2',
{
controller: 'SimpleController',
templateUrl: 'View2.html'
})
.otherwise({ redirectTo: '/view1' });
});
demoApp.factory('simpleFactory', function () {
var factory = {};
factory.getAnnounces = function ($http) {
$http.post("http://localhost:57034/Announce/GetAllAnnounces")
.success(function (data, status, headers, config) {
return data;
}).error(function (data, status, headers, config) {
return status;
});
};
return factory;
});
demoApp.controller('SimpleController', function ($scope,simpleFactory) {
$scope.announces = [];
init();
function init()
{
$scope.announces= simpleFactory.getAnnounces();
}
});
What am I missing here? Cheers.

You need to review your code as follows:
demoApp.factory('simpleFactory', ['$http', function ($http) {
return {
getAnnounces: function () {
$http.post("http://localhost:57034/Announce/GetAllAnnounces")
.success(function (data, status, headers, config) {
return data;
}).error(function (data, status, headers, config) {
return status;
});
}
};
}]);
There's no need to pass the $http variable in the getAnnounces method definition, because it is defined already in the scope of the factory function.
I am using parameter aliasing for AngularJS in order to avoid issues with minifiers, see 'A note on minification' on the AngularJS web site.
Note anyway that $http.post.success and $http.post.error are asynchronous and you won't be able to get the data unless you're using promises ($q), see here. Therefore you could change the code this way:
demoApp.factory('simpleFactory', ['$http', '$q', function ($http, $q) {
return {
getAnnounces: function () {
var deferred = $q.defer();
$http.post("http://localhost:57034/Announce/GetAllAnnounces")
.success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (data, status, headers, config) {
deferred.reject(data);
});
return deferred.promise;
}
};
}]);
And in the SimpleController:
demoApp.controller('SimpleController', ['simpleFactory', '$scope', function (simpleFactory, $scope) {
$scope.announces = [];
simpleFactory.getAnnounces()
.then(function(data) {
// call was successful
$scope.announces = data;
}, function(data) {
// call returned an error
$scope.announces = data;
});
}]);

Related

angular ionic $http service not working

Loading json using $http isn't working. Can't figure out where the problem is
.factory('streamStore', function ($http) {
var cachedData = null;
return {
findAll: function getAll() {
$http.get('img/sample.json').
success(function (data, status, headers, config) {
console.log("inside stream controller");
cachedData = data; return cachedData;
}).
error(function (data, status, headers, config) {
// log error
});
}
And in my controller this is how I'm calling it:
.controller('streamCtrl', function ($scope, ,streamStore) {
$scope.streams = streamStore.findAll();//not working ,
There is a error in your code . Check below
.factory('streamStore', function ($http) {
var cachedData = null;
return {
findAll: function() {
$http.get('img/sample.json').
success(function (data, status, headers, config) {
console.log("inside stream controller");
cachedData = data;
return cachedData;
}).
error(function (data, status, headers, config) {
// log error
});
}
and then call , as you were calling in your controller.
or use this
.factory('streamStore', function ($http) {
var cachedData = function(){
return $http.get('img/sample.json');
};
return {
findAll: function(){
return cachedData();
}
}
});
then in controller
streamStore.findAll().then(function(response){
console.log(response) // here is your data
})
since you have not returned anything from factory.
.factory('streamStore', function ($http) {
var cachedData = null;
return {
findAll: function getAll() {
$http.get('img/sample.json').
success(function (data, status, headers, config) {
console.log("inside stream controller");
cachedData = data; return cachedData;
// should return something
return cachedData
}).
error(function (data, status, headers, config) {
// log error
});
}
one more suggestion return promise to make it safe .
return $http.get('img/sample.json')...
and handle with then in controller.
.controller('streamCtrl', function ($scope, ,streamStore) {
streamStore.findAll().then(function(res){
//res will contain your data
$scope.streams = res.data;
});

deferred.resolve giving error TypeError: undefined is not a function

I am using angularjs and cordova tool for creating application.
I have created service, which contains code for calling APIs. and in that I want to return response to my angular controller.
My code is,
Service,
JodoModule.factory('commonServices', function ($http, $q, $rootScope) {
return {
getServiceData: function (url) {
$rootScope.loading = true;
var deferred = $q.defer();
var req = {
method: 'GET',
url: url
}
$http(req).success(function (data) {
alert("data in service = " + JSON.stringify(data.Data));
deferred.resolve(data);
}).error(function (data, status, headers, config) {
deferred.reject(status);
});
return deferred.promise;
}
};
});
My controller is,
commonServices.getServiceData("My url").
success(function (data, status, headers, config) {
alert(data);
}).
error(function (data, status, headers, config) {
alert("Got error");
});
In above code, in service, its showing elert message for JSON.stringify(data.Data)); in success block, so data is comming, but its not executing deferred.resolve(data); properly...
in Web tool bar its giving me error,
ie. TypeError: undefined is not a function
My o/p is :
{"status":"SUCCESS","Message":"success","Token":"","Data":[{"Id":17,"UserId":"477f1919-6b80-4804-a325-ac0cb05bcd3e","UserName":"honey","FirstName":"honey","LastName":null,"ProfilePic":false,"Status":2}]}
How can I solve this error. ?
Ordinary $q promises don't have a .success() or .error() method, but you shouldn't be using the deferred antipattern anyway. Instead, do this:
JodoModule.factory('commonServices', function ($http, $rootScope) {
return {
getServiceData: function (url) {
$rootScope.loading = true;
var req = {
method: 'GET',
url: url
};
return $http(req).then(function (result) {
alert("data in service = " + JSON.stringify(result.data.Data));
return result.data;
});
}
};
});
Controller:
commonServices.getServiceData("My url").
then(function (data) {
alert(data);
}).
catch(function (result) {
alert("Got error");
});
Quite a bit cleaner, ay?

AngularJS : service not returning value

I'm trying to write an Angular service and it seems like there is something missing. My problem is its not returning any value to my Angular controller
getPrepTimes() method is not returning the http data
But when I check the network (via Chrome dev tools) it will correctly call the external api and return a json object as a response
#my service
'use strict';
angular.module('recipeapp')
.service('prepTimeService',['$http', function($http){
this.prepTime = getPrepTimes();
function getPrepTimes(){
$http({
url: '/prep_times/index.json',
method: 'GET'
})
.success(function (data, status, header, config){
return data;
});
};
}
]);
#controller
'use strict';
angular.module('recipeapp')
.controller('recipeCtrl', ['$scope', 'prepTimeService', function($scope, prepTimeService){
$scope.prep_time = prepTimeService.prepTime;
}]);
When I checked the method getPrepTimes() with returning a string it works. What could be missing here?
A couple things are wrong with the above. You assign this.prepTime to getPrepTimes(). The () there will invoke getPrepTimes immediately, and not when you actually call it! You also need to utilize callbacks to get your data back and use it:
angular.module('recipeapp').service('prepTimeService',['$http', function($http){
this.prepTime = getPrepTimes;
function getPrepTimes(callback) {
$http({
url: '/prep_times/index.json',
method: 'GET'
}).success(function (data, status, header, config){
callback(data);
});
};
}]);
And now use it like so:
prepTimeService.prepTime(function(data) {
$scope.prep_time = data;
});
Calls to the $http service are async, which means you need to return a promise (and not a value):
this.prepTime = function() {
return $http({
url: '/prep_times/index.json',
method: 'GET'
});
};
And on the controller:
angular.module('recipeapp')
.controller('recipeCtrl', ['$scope', 'prepTimeService', function($scope, prepTimeService){
$scope.prep_time = prepTimeService.prepTime()
.success(function (data, status, header, config){
$scope.someVar = data;
});
}]);
Wrap answer with promise:
var self = this;
var deferred = $q.defer();
self.getPrepTimes = function() {
$http({
url: '/prep_times/index.json',
method: 'GET'
})
.success(function(data, status, headers, config) {
if (data.error === undefined) {
deferred.resolve(data);
} else {
if (data.error !== undefined) {
} else {
deferred.reject(data);
}
}
}).error(function(data, status, headers, config) {
deferred.reject(data);
});
return deferred.promise;
};
In controller call it:
prepTimeService.getPrepTimes().then(function(result) {
$scope.prep_time = result;
},
function(error) {
// show alert
});

Angular ui-router get asynchronous data with resolve and promise/deferred

I've having a problem to get the resolve mechanism working in my application.
I separated the webservice call into an extra module and using deferred/promise to have callbacks.
Before showing the state "workflowdefinitions.detail", the app should load the workflow definition be using the workflowDefinitionId of the $stateParams and call the function "getWorkflowDefinition" of the workflowDefinitionService at the service module.
I tried out multiple things that I had read here, but can't get it working. How do I need to handle the returned promise to pass the return data to the workflowDefinition defined by resolve?
Can this work with my services or do I have to define the service in a different way?
app.js
var atpApp = angular.module('atpApp', [ 'ui.router', 'workflowServices', 'workflowControllers' ]);
atpApp.config([ '$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider , $locationProvider) {
$urlRouterProvider.otherwise('/workflowdefinitions');
$stateProvider.state('workflowdefinitions', {
url : '/workflowdefinitions',
controller : 'WorkflowDefinitionListCtrl',
templateUrl : 'partials/workflowdefinition-list.html'
})
.state('workflowdefinitions.detail', {
url : '/:workflowDefinitionId',
views : {
'#' : {
templateUrl : 'partials/workflowdefinition-detail.html',
controller : 'WorkflowDefinitionDetailCtrl',
resolve: {
workflowDefinition: function($stateParams, workflowDefinitionService) {
return workflowDefinitionService.getWorkflowDefinition($stateParams.workflowDefinitionId);
}
}
}
}
});
} ]);
atpApp.run([ '$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
} ]);
Module for Services (workflowSevices.js)
var workflowServices = angular.module('workflowServices', []);
workflowServices.service('workflowDefinitionService', function($http, $q) {
var config = {headers: {
'Accept': 'application/json'
}
};
this.getWorkflowDefinitions = function(){
var deferred = $q.defer();
$http.get('http://localhost:8080/vms-atp-webapp/services/rest/workflows', config).
success(function(data, status) {
deferred.resolve(data);
}).error(function(data, status) {
deferred.reject(data);
});
return deferred.promise;
};
this.getWorkflowDefinition = function(workflowDefinitionId){
var deferred = $q.defer();
$http.get('http://localhost:8080/vms-atp-webapp/services/rest/workflows/'+workflowDefinitionId, config).
success(function(data, status) {
deferred.resolve(data);
}).error(function(data, status) {
deferred.reject(data);
});
return deferred.promise;
};
this.activateWorkflowDefinition = function(workflowDefinitionId){
var deferred = $q.defer();
$http.post('http://localhost:8080/vms-atp-webapp/services/rest/workflows/'+workflowDefinitionId+"/activate", config).
success(function(data, status) {
deferred.resolve(data);
}).error(function(data, status) {
deferred.reject(data);
});
return deferred.promise;
};
this.deactivateWorkflowDefinition = function(workflowDefinitionId){
var deferred = $q.defer();
$http.post('http://localhost:8080/vms-atp-webapp/services/rest/workflows/'+workflowDefinitionId+"/suspend", config).
success(function(data, status) {
deferred.resolve(data);
}).error(function(data, status) {
deferred.reject(data);
});
return deferred.promise;
};
});
This concept should be working. There is a plunker, which should be doing almost the same you've tried above. No changes, as is. (as is in the code above)
The only change - for example purposes - is the service method getWorkflowDefinition, which does delay because of $timeout service, but then returns the param passed
this.getWorkflowDefinition = function(param){
var deferred = $q.defer();
$timeout(function(){
deferred.resolve(param);
}, 750)
return deferred.promise;
};
So, your concept, design is working, check more here: plunker
Additionally you don't need the boiler plate for deferred/resolve everywhere.
This code
var deferred = $q.defer();
$http.post( 'http://localhost:8080/vms-atp-webapp/services/rest/workflows/' + workflowDefinitionId +"/suspend", config).
success(function(data, status) {
deferred.resolve(data);
}).error(function(data, status) {
deferred.reject(data);
});
return deferred.promise;
can be simplified to
simple
return $http.get('http://localhost:8080/vms-atp-webapp/services/rest/workflows', config);
This is because the $http.get returns a promise which when fulfilled is internally resolved/rejected on success and error.

Initializing AngularJS service factory style

I have a service that retrieves data via REST. I want to store the resulting data in service level variable for use in multiple controllers. When I put all the REST logic directly into controllers everything works fine but when I attempt to move the retrieval / storing of data into a service the controller is not being updated when the data comes back. I've tried lots of different ways of maintain the binding between service and controller.
Controller:
myApp.controller('SiteConfigCtrl', ['$scope', '$rootScope', '$route', 'SiteConfigService',
function ($scope, $rootScope, $route, SiteConfigService) {
$scope.init = function() {
console.log("SiteConfigCtrl init");
$scope.site = SiteConfigService.getConfig();
}
}
]);
Service:
myApp.factory('SiteConfigService', ['$http', '$rootScope', '$timeout', 'RESTService',
function ($http, $rootScope, $timeout, RESTService) {
var siteConfig = {} ;
RESTService.get("https://domain/incentiveconfig", function(data) {
siteConfig = data;
});
return {
getConfig:function () {
console.debug("SiteConfigService getConfig:");
console.debug(siteConfig);
return siteConfig;
}
};
}
]);
View:
<div class="span4" ng-controller="SiteConfigCtrl">
<header>
<h2>
{{site.title}}
</h2>
</header>
I would write it with promise factory:
myApp.factory('SiteConfigService', ['$http', '$rootScope', '$timeout', 'RESTService', '$q'
function ($http, $rootScope, $timeout, RESTService, $q) {
var siteConfig = {} ;
RESTService.get("https://domain/incentiveconfig", function(data) {
siteConfig = data;
});
// or just
// var siteConfig = RESTService.get("https://domain/incentiveconfig");
return {
getConfig:function () {
var deferred = $q.defer();
deferred.resolve(siteConfig);
return deferred.promise;
}
};
}
]);
Controller side
SiteConfigService.getConfig()
.then(function (result) {
$scope.site = result;
}, function (result) {
alert("Error: No data returned");
});
Solution based on Maxim's answer above - JsFiddle - http://jsfiddle.net/acb98sm/2pQ6A/6/
var myApp = angular.module('myApp',[]);
myApp.controller('SiteConfigCtrl', ['$scope', '$rootScope', '$route', 'SiteConfigService',
function ($scope, $rootScope, $route, SiteConfigService) {
SiteConfigService.getConfig()
.then(function (result) {
console.log("results are in ");
console.log(result);
$scope.site = result.data;
}, function (result) {
alert("Error: No data returned");
});
}
]);
myApp.factory('SiteConfigService', ['$http', '$rootScope', '$timeout', 'RESTService', '$q',
function ($http, $rootScope, $timeout, RESTService, $q) {
var siteConfigFn = RESTService.get("http://graph.facebook.com/616366118/", function(data) {
console.log("SiteConfigService returns");
});
return {
getConfig:function () {
var deferred = $q.defer();
deferred.resolve(siteConfigFn);
return deferred.promise;
}
};
}
]);
myApp.$inject = ['$scope', 'SiteConfigService', 'RESTService'];
myApp.factory('RESTService',
function ($http) {
return {
get:function (url, callback) {
return $http.get(url, {withCredentials:false}).
success(function (data, status, headers, config) {
callback(data);
}).
error(function (data, status, headers, config) {
console.log("failed to retrieve data");
});
},
post:function (url, data, callback) {
return $http.post(url, data, {withCredentials:true}).
success(function (data, status, headers, config) {
callback(data);
}).
error(function (data, status, headers, config) {
console.log("failed to retrieve data");
});
}
};
}
);

Resources