angularjs-service

How to use angularJS interceptor to only intercept specific http requests?

大憨熊 提交于 2019-11-30 10:47:31
问题 I know how to intercept ALL requests, but I only want to intercept requests from my resources. Does anyone know how to do this? services.config(['$httpProvider',function($httpProvider) { $httpProvider.interceptors.push('myHttpInterceptor'); }]); services.factory("userPurchased", function ($resource) { return $resource("/api/user/purchases/:action/:item", {}, { 'list': {method: 'GET', params: {action: 'list'}, isArray: false}, 'save': {method: 'PUT', params: {item: '@item'}}, 'remove': {method

Passing/Updating Data in a Factory from One Controller to Another

若如初见. 提交于 2019-11-30 09:45:46
问题 I'm not sure if I am having a "best practices" issue where I am trying to solve this challenge in a weird and wonderful way or wether my grip on AngularJS just isn't there yet. Scenario : So I have been trying to come up with a way to have "originCity.cityName" be output on the page a few times, all referencing the same object inside the same factory with the hope that if I set it to some other value ("Test String" for example) then it would retroactively replace all of the {{originCity

Call controller function from service in angularjs

无人久伴 提交于 2019-11-30 08:51:52
I am using socket.io to enable chat in my app and i am using a service SocketService to perform all the socket stuff. When a message came then i want to trigger a function of a controller from the service SocketService to make some changes in the UI. So i want to know that how can i access the function of a controller from the service. Sample Code: .service('SocketService', function ($http,$rootScope,$q) { this.connect = function(){ var socket = io(); socket.on('connect',function(){ // Call a function named 'someFunction' in controller 'ChatController' }); } }); This is the sample code for

Sharing data between AngularJS services

╄→гoц情女王★ 提交于 2019-11-30 07:44:05
Is there a way to share data between services in AngularJS? Use case: data aggregation from different services For example I want a service1 that loads some data from a REST Service. Then another service2 adds additional data to the service1 data from another REST API to create a data aggregation service. I basically want to separate the services based on the APIs that they use, but still have a service that holds all the data in the end. Create a third service that uses the $q deferred library to combine the promises from the 2 API REST calls using $q.all() . Then pass this third service to

AngularJS : returning data from service to controller

一世执手 提交于 2019-11-30 07:18:23
I am trying to create a service to get json and pass it to me homeCtrl I can get the data but when a pass it to my homeCtrl it always returns undefined. Im stuck. My Service: var myService = angular.module("xo").factory("myService", ['$http', function($http){ return{ getResponders: (function(response){ $http.get('myUrl').then(function(response){ console.log("coming from servicejs", response.data); }); })() }; return myService; } ]); My Home Controller: var homeCtrl = angular.module("xo").controller("homeCtrl", ["$rootScope", "$scope", "$http", "myService", function ($rootScope, $scope, $http,

Angular - TypeError: XX is not a function

為{幸葍}努か 提交于 2019-11-30 07:12:10
问题 Maybe I'm missing some kind of property, but I'm follow this project and I'm getting this error in my controller. TypeError: loginService.signin is not a function This is my controller.js angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService', function ($rootScope, $scope, $http, loginService) { $scope.signin = function() { console.log("username: " + $scope.username); console.log("pass: " + $scope

How can I access and test an AngularJS filter from the browser console?

纵饮孤独 提交于 2019-11-30 05:28:24
问题 Given a test filter, say this 'capitalize' filter that will capitalize the first letter of each word: return function (input) { return (!!input) ? input.replace(/([^\W_]+[^\s-]*) */g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }) : ''; } How can this filter be tested from a browser's JavaScript console? 回答1: Filters can be called in HTML template binding {{myString | capitalize}}, but to gain access to it in the browser we have an excellent option.

AngularJS [$injector:unpr] Unknown provider

做~自己de王妃 提交于 2019-11-30 03:36:28
问题 I am trying to inject a service into controller, and i am getting following error: Error: [$injector:unpr] Unknown provider: employeeServiceProvider <- employeeService http://errors.angularjs.org/1.3.0-beta.17/$injector/unpr?p0=employeeServiceProvider%20%3C-%20employeeService at http://localhost:9082/angularJSDemo/js/lib/angular.js:78:12 at http://localhost:9082/angularJSDemo/js/lib/angular.js:3894:19 at Object.getService [as get] Here is plunker for code. Any help would be appreciated. 回答1:

How can I extend $q promise in Angularjs with a .success and .error

本秂侑毒 提交于 2019-11-30 03:05:24
I wrote this little code in a custom service in AngularJS. In my service: var deferred = $q.defer(); var promise = deferred.promise; deferred.resolve('success'); deferred.reject('error'); /* Handle success and error */ promise.success = function(fn) { promise.then(function(response) { fn(response); }); return promise; }; promise.error = function(fn) { promise.then(null, function(response) { fn(response); }); return promise; }; In my controller: promiseService.myPromise() .success(function(data){ $scope.success= data; }) .error(function(data){ $scope.error = data; }); I juste Handle the Success

How to use angularJS interceptor to only intercept specific http requests?

眉间皱痕 提交于 2019-11-29 21:25:31
I know how to intercept ALL requests, but I only want to intercept requests from my resources. Does anyone know how to do this? services.config(['$httpProvider',function($httpProvider) { $httpProvider.interceptors.push('myHttpInterceptor'); }]); services.factory("userPurchased", function ($resource) { return $resource("/api/user/purchases/:action/:item", {}, { 'list': {method: 'GET', params: {action: 'list'}, isArray: false}, 'save': {method: 'PUT', params: {item: '@item'}}, 'remove': {method: 'DELETE', params: {item: '@item'}}, } ); }); services.factory('myHttpInterceptor', function($q,