angularjs-service

Combining resources in AngularJS

无人久伴 提交于 2019-12-04 14:25:50
问题 I have a RESTful API that provides me with employees and departments. In my AngularJS app, I need to know the employee and the departments the employee belongs to. I have defined two factory services for that: angular.module('myApp.services', []) .factory('Employee', function ($resource) { return $resource ('/api/employees/:id', { id: '@id' }); }) .factory('Department', function ($resource) { return $resource ('/api/departments/:id', { id: '@id' }); }) ; In the controller, I call: $scope

Calling only once / caching the data from a $http get in an AngularJS service

末鹿安然 提交于 2019-12-04 13:26:31
问题 This may sound like a really simply/stupid question but I need to ask it as I haven't came across this scenario before... okay I have a service in my angularJS app. this service currently contains 4 methods that all perform 80% the same functionality/code and I wish to make this more efficient. Here is what my service looks like (with a lot of code removed): .factory('townDataService', function ($http) { var townList = {}; townList.getTownList = function () { return $http({method: 'GET', url:

How to check authentication and automatically redirect to login state with ui-router?

走远了吗. 提交于 2019-12-04 12:41:27
so I have this function in my run in angularjs (running AngularJS v1.2.26) .run(function(....) { authService.authCheck(); $rootScope.$on('$routeChangeStart', function(event, next, current) { if (next.requireLogin) { if (!authService.isLoggedIn()) { $location.path('/login'); event.preventDefault(); } } }); })... when directly accessing a URL that requires authentication e.g. http://127.0.0.1:8080/secret , the function always redirects to the login path, clearly after debugging it for a while it evaluates to false all the time. when accessing default page then going to the specified route it

AngularJS: How to share scope functions and variables with other controllers

℡╲_俬逩灬. 提交于 2019-12-04 11:27:08
I've multiple controllers in my application, where I have some duplicate code like: $scope.alert = null; $scope.addAlert = function (message) { $scope.alert = { type: 'danger', msg: message }; }; $scope.clearAlerts = function () { $scope.alert = null; }; What is the recommended way sharing these scope functions and variables in AngularJS? Using controller inheritance? Create a one controller and then place common methods inside that controller scope. So that you can use that scope anywhere else and get access to method inside controller. Controller app.controller('commonCtrl', function($scope)

Firebase's AngularFire in an AngularJS service

本小妞迷上赌 提交于 2019-12-04 11:00:27
问题 The best way of handling Firebase in AngularJS surely has to be from within a service, so it's available to all Controllers across the App. I just can't get it to work! ... I first tried using angularFire(new Firebase(url)) , hoping I could bind to the service's scope, but Angular complains that it cannot $watch it. So I tried angularFireCollection instead like this: app.factory('myService', function myService(angularFireCollection) { var url = 'https://myfirebase.firebaseio.com'; return {

AngularJS Informer service

非 Y 不嫁゛ 提交于 2019-12-04 08:57:50
I'm using Twitter Bootstrap for UI in my webapp. Particulary its Alert component. I want to write a simple angular service to wrap Bootstrap's Alert to have a possibility of informing users from any peace of angular code. Like this: Informer.inform("message", "ERROR"); // will result in alerting with `alert-error` class Informer.inform("message", "INFO"); // will result in alerting with `alert-info` class My idea is to to append the template to the end of the <body> : <div class="alert {{alertClass}} fade in informer" id="informer"> <button type="button" class="close" data-dismiss="alert">×<

How to set a timeout to abort an $http.get() inside a factory or service?

和自甴很熟 提交于 2019-12-04 04:07:09
I have the following method getData(url) in a my factory which uses $http.get(url) to get data from an URL angular .module('az-app') .factory('WebServiceFactory', function ($http, $q) { var WebServiceFactory = this; WebServiceFactory.getData = function (url) { var deferred = $q.defer(); $http.get(url) .then( function (response) { deferred.resolve({ data: response }); }, function (rejected) { deferred.reject({ data: rejected }); } ); //Promise to be returned return deferred.promise; } It works fine but I need to abort the http.get and/or reject the promise so I can display an error message from

Unknown provider CookieStore

我怕爱的太早我们不能终老 提交于 2019-12-04 02:49:32
I am having service where I want to use the $cookieStore module. It works fine, but when unit testing it breaks, and gives the error: "$cookieStoreProvider <- $cookieStore <- filtersService". The service looks like this: serviceModule.factory('filtersService', ['$rootScope', '$location', '$cookieStore', function($rootScope, $location, $cookieStore){ return { getFilters: function(){...} } And the unit test service looks like this: describe('filtersService tests', function(){ var filtersService; beforeEach(module('App.services')); beforeEach(inject(function(filtersService, urlService, $location)

Inject dateFilter in a service in AngularJs

。_饼干妹妹 提交于 2019-12-03 22:04:15
I would like to know if there is a way to inject the filters in a service in AngularJs. I've been trying app.factory('educationService', [function($rootScope, $filter) { // ..... Some code // What I want console.log(dateFilter(new Date(), 'yyyy-MM-01')); // ..... Some code }]); So I would like to know if it is possible to inject a filter in a service or maybe it is accessible in another way. And if you have a link to a documentation about this point, it would be really nice :) Been searching in the doc of Angular and I found nothing really helpful about this. Thanks :) Stewie First you have to

AngularJS: How to handle success and error call backs with ngResource?

守給你的承諾、 提交于 2019-12-03 16:12:39
问题 The docs does not give any idea about it. My REST enpoint might throw error $scope.delete = function(index) { Transaction.delete({transactionId: $scope.transactions[index].uuid}) }; I changed the above to following $scope.delete = function(index) { Transaction.delete({transactionId: $scope.transactions[index].uuid}) .success('transaction deleted'); }; But it fails TypeError: Object #<Resource> has no method 'success' at Object.TransactionController.$scope.delete (http://localhost:5000/static