Trying to inject $ionicHistory into service itself.
Can I write like this?
.service('$ionicUtilityService', ['CONSTANT', '$log', '$ionicHistory', function(CONSTANT, $log, $ionicHistory) {
var apis = {};
apis.clearNavigationHistory = function() {
$ionicHistory.clearHistory();
};
return apis;
}]);
I am getting error
Error: $ionicHistory is undefined
apis.clearNavigationHistory#http://localhost:8100/js/services.min.js:1:6223
#http://localhost:8100/components/customer/home/home-controller.min.js:1:206
jf/this.$gethttp://localhost:8100/lib/ionic/js/ionic.bundle.min.js:170:424
D.create/O.emit#http://localhost:8100/lib/ionic/js/ionic.bundle.min.js:446:19204
D.create/O.transition#http://localhost:8100/lib/ionic/js/ionic.bundle.min.js:446:18728
Have you tried injecting it elsewhere?
I tried this:
.factory('MyService', function(TestService, $ionicHistory)
{
var service {
myMethod: function(){$ionicHistory.goBack()}
}
return service
}
And got no errors.
I have a basic data Service which will be used across Controllers. But I'm having an issue grabbing some data that's been added via $http.
Service:
angular.module('core').service('FormService', ['$http', function($http) {
var _this = this;
_this.dropdownData = {
contactTimes: ['Anytime','Morning','Afternoon','Evening'],
industries: {},
};
$http.get('/json').success(function(resp){
_this.dropdownData.industries = resp.industries;
});
}]);
Controller:
angular.module('core').controller('SignupController', ['$scope', '$http', '$state', 'FormService', function($scope, $http, $state, FormService) {
console.log(FormService.dropdownData); // Shows full object incl industries
console.log(FormService.dropdownData.industries); // empty object {}
}]);
How do I get FormService.dropdownData.industries in my controller?
Create a service like below
appService.factory('Service', function ($http) {
return {
getIndustries: function () {
return $http.get('/json').then(function (response) {
return response.data;
});
}
}
});
Call in controller
appCtrl.controller('personalMsgCtrl', ['$scope', 'Service', function ($scope, Service) {
$scope.Industries = Service.getIndustries();
}]);
Hope this will help
Add a method to your service and use $Http.get inside that like below
_this.getindustries = function (callback) {
return $http.get('/json').success(function(resp){
_this.dropdownData.industries = resp.industries;
callback(_this.dropdownData)
});
};
In your controller need to access it like below.
angular.module('core').controller('myController', ['$scope', 'FormService', function ($scope, FormService) {
FormService.getDropdownData(function (dropdownData) {
console.log(dropdownData); // Shows full object incl industries
console.log(dropdownData.industries); // object {}
});
} ]);
Given that your console log shows the correct object, that shows your service is functioning properly. Only one small mistake you have made here. You need to access the data attributes in your return promise.
angular.module('core').service('FormService', ['$http', function($http) {
var _this = this;
_this.dropdownData = {
contactTimes: ['Anytime','Morning','Afternoon','Evening'],
industries: {},
};
$http.get('/json').success(function(resp){
//note that this is resp.data.industries, NOT resp.industries
_this.dropdownData.industries = resp.data.industries;
});
}]);
Assuming that you're data is indeed existing and there are no problems with the server, there are quite a few possible solutions
Returning a promise
angular.module('core').service('FormService', ['$http', function($http) {
var _this = this;
_this.dropdownData = {
contactTimes: ['Anytime','Morning','Afternoon','Evening'],
industries: {},
};
_this.dropdownData.industries = $http.get('/json');
}]);
//Controller
FormService.industries
.then(function(res){
$scope.industries = res.industries
});
Resolving with routeProvider / ui-route
See: $http request before AngularJS app initialises?
You could also write a function to initialize the service when the application starts running. At the end of the day, it is about waiting for the data to be loaded by using a promise. If you never heard about promises before, inform yourself first.
The industries object will be populated at a later point in time when the $http call returns. In the meantime you can still bind to the reference in your view because you've preserved the reference using angular.copy. When the $http call returns, the view will automatically be updated.
It is also a good idea to allow users of your service to handle the event when the $http call returns. You can do this by saving the $promise object as a property of industries:
angular.module('core').service('FormService', ['$http', function($http) {
var _this = this;
_this.dropdownData = {
contactTimes: ['Anytime','Morning','Afternoon','Evening'],
industries: {},
};
_this.dropdownData.industries.$promise = $http.get('/json').then(function(resp){
// when the ansyc call returns, populate the object,
// but preserve the reference
angular.copy( resp.data.industries, _this.dropdownData.industries);
return _this.dropdownData.industries;
});
}]);
Controller
app.controller('ctrl', function($scope, FormService){
// you can bind this to the view, even though the $http call has not returned yet
// the view will update automatically since the reference was preserved
$scope.dropdownData = FormService.dropdownData;
// alternatively, you can hook into the $http call back through the $promise
FormService.dropdownData.industries.$promise.success(function(industries) {
console.log(industries);
});
});
I keep getting errors while injecting a factory in to a controller. It worked in a case where the controller is simple. It fails when the page and the controller has special components like ng-flow or ng-grid.
var carouselController = carouselApp.controller('carouselController', ['CMSFactory', function ($scope,CMSFactory) {
// MY CODE HERE...
}
var CMSServices = angular.module('api.services', [ 'ngResource' ]);
CMSServices.factory('CMSFactory', function($http, $q) {
var CMSService = {};
CMSService.saveSiteInfo = function(data) {
// MY CODE HERE...
};
CMSService.addSlide = function(data) {
// MY CODE HERE...
};
return CMSService;
});
I get TypeError: undefined is not a function error. If I remove the factory injection code works fine.
Appreciate any help...
You're not declaring $scope in your array of dependencies:
Change:
var carouselController = carouselApp.controller('carouselController', ['CMSFactory', function ($scope,CMSFactory) {
To:
var carouselController = carouselApp.controller('carouselController', ['$scope', 'CMSFactory', function ($scope,CMSFactory) {
Trying to get ngResource to work, but getting the error:
Object # has no method 'query'
I've tried to keep it as simple as possible and according to the documentations/posts that I can find, this should work. But it doesn't.
Here's my service/factory:
var srcServices = angular.module('srcServices', ['ngResource']);
srcServices.factory('Rotation', ['$resource',
function ($resource) {
return $resource('/rotations/:id' );
}]);
And here's the controller code triggering the error:
var srcControllers = angular.module('srcControllers', ['ngResource']);
srcControllers.controller('RotationListCtrl', ['$scope', '$location', 'Rotation', function($scope, Rotation, $location) {
Rotation.query(function(data) {$scope.rotations = data;})
$scope.edit = function(a) {
var path = "/rotations/" + a._id.$oid;
$location.path(path);
};
}]);
Try this
var srcControllers = angular.module('srcControllers', ['ngResource', 'srcServices']);
You are creating the service as a new angular module. So in order to get access to the service you have to inject it to your controller.
Here is my scenario. I want to refresh my captcha if the form get error message. And I have multi views to use the captcha.
So I write a factory:
Services.factory 'Captcha', ['$rootScope', ($rootScope) ->
service = {}
service.new_captcha = () ->
console.log 'render cap'
$rootScope.captcha_src = "/captcha?action=captcha&i=#{+new Date}"
service
]
And then another factory where handle the $http process will trigger the code below
$http
.error (data) ->
service.signin_err_msg = data.error
Captcha.new_captcha()
$rootScope.$broadcast('new_captcha')
In the view controller, $scope value will listen to the broadcast and change the src value.
SignUpCtrl = App.controller 'SignUpCtrl', ($scope, UserService, $location, $rootScope) ->
$scope.UserService = UserService
$scope.$on 'new_captcha', (val) ->
$scope.captcha_src = $rootScope.captcha_src
$scope.captcha_src = $rootScope.captcha_src
This works. But I dont think this is a good way. I have to write the same code to listen the rootScope broadcast. Is there same method better?
As rtcherry says, you don't need to use $rootScope.
Please have a look at this Plunker: http://embed.plnkr.co/4ppfCi/preview
There is no need to use $rootScope or to broadcast any events. This would also work:
Captcha Service
Services.factory('Captcha', [function() {
var captchaSrc;
return {
get_captcha_src: function() {
if (!captchaSrc) {
this.refresh_captcha();
}
return captchaSrc;
},
refresh_captcha: function() {
captchaSrc = "/captcha?action=captcha&i=" + new Date();
}
}
}]);
HTTP Handler
$http.error(function(data) {
service.signin_err_msg = data.error;
Captcha.refresh_captcha();
});
Controller
var SignUpCtrl = App.controller('SignUpCtrl', function($scope, $location, UserService, Captcha) {
...
$scope.get_captcha_src = function() {
return Captcha.get_captcha_src();
}
// or this: $scope.get_captcha_src = Captcha.get_captcha_src;
});