FAQ Database Discussion Community


How to inject module and make it accesible to entrie angular app

angularjs,angular-services
I have a module (app.config) that I would like to inject my entire app. The module needs to be accessible within all other modules injected into the app For example, my app looks like this: angular.module('myApp', [ 'app.config', 'module#1', 'module#2', 'module#3', 'module#4' ]) .config... ///////////////////////////////// Here's app.config angular.module('app.config', []). constant('NAME1',...

Can you pass an Angular service as a parameter to a function provided by another Angular service?

javascript,angularjs,refactoring,angular-services
I have a bunch of functions throughout my Angular app using mostly the same code. So I decided to create a service which is made of the frequently duplicated code, and takes as parameters the differences. One of the primary and few differences each of the duplicate functions have is...

Angular Service JSONP Request with Custom Callback

angularjs,jsonp,angular-services
I'm pulling from JSONP feeds that have custom callback functions, for example: jsonpCallbackAllStar2015({ "events": [ { "title": "XYZ" } ... ] }) I'm able to do this, using the solution posted here like so: var jsonUrl = 'http://i.cdn.turner.com/nba/nba/.element/media/2.0/teamsites/warriors/json/json-as2015.js?callback=JSON_CALLBACK' + (new Date().getTime()); $http.jsonp(jsonUrl); window.jsonpCallbackAllStar2015 = function(data) { $scope.events = data.events; }...

Restangular - Specify custom ID key

javascript,angularjs,restangular,angular-services
In Restangular if I declare a service, and do a PUT/PATCH/POST operation it uses the id of the item by default as a primary key. But what if we want to use a custom key? Like a slug or a number ? // GET to /users Users.getList().then(function(users) { var user...

AngularJS factory inside ng-class on body tag not working

angularjs,controller,factory,angular-services,ng-class
Was told to create service/factory and use it on global controller. It's my first time hearing global controller. Anyways, I created a factory called Scroll and injected it on our main.controller.js. The function in the factory is isScrollingEnabled which returns true or false. The other function is setScrolling which will...

Angular development and using services into run app

angularjs,angular-ui,interceptor,angular-services
I was wondering if anyone could help me understand why i can't get my code works. I have put a session check in the run method from my main module, everything for the checking part works well but for some reasons when i try to launch the modal to reconnect...

Optimize ajax calls with AngularJS [duplicate]

angularjs,angular-services,angularjs-http
This question already has an answer here: how do I trigger an angularjs service after every nth seconds a user typed something 3 answers I'm creating something like this example but for search proposes, using this code: $scope.$watch('terms', function(newValue, oldValue) { if (angular.equals(newValue, oldValue)) { return; } getResults(); //$http.get......

Why do angular service “private” fields not update?

angularjs,angular-services
If I follow this particular practice of making factories: myApp.factory('myService', function () { var somevalue = 2; var myServiceApi = { theValue: somevalue, updatevalue: updateValue } return myServiceApi; function updateValue(newValue) { somevalue = newValue; } }); Each and every time the service is injected the value of somevalue is always...

keep data in controller while routing in angular

angularjs,angular-services,angularjs-ng-route
I have following problem: In AngularJS app I use $routeProvider to load different parts into application config(['$routeProvider', function($routeProvider) { $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'}); $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'}); $routeProvider.when('/users', {templateUrl: 'partials/users.html', controller: 'UserCtrl'}); $routeProvider.otherwise({redirectTo: '/view1'}); }]); In my UserCtrl I display list of users, which I can...

Angular service field is undefined for one controller only

javascript,angularjs,dependency-injection,controllers,angular-services
I have defined a simple service: app.service('AuthenticationService', function() { var auth = { isLogged: false }; return auth; }); I use this to set and share the authentication state between controllers. It reads fine in my LoginCtrl: app.controller('LoginCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService', function LoginCtrl($scope, $location, $window, UserService, AuthenticationService) {...

Angularjs Factory not loading when added as dependency in Controller

javascript,angularjs,angular-services
I am very new to Angularjs and this is my first try to build an app with it. I've following file loaded in my index.html file. <!--Angularjs--> <script src="assets/js/vendor/angular.min.js" type="text/javascript"></script> <script src="assets/js/vendor/angular-route.min.js" type="text/javascript"></script> <!--SleekDocketAppJS--> <script src="app/app.module.js" type="text/javascript"></script> <script src="app/app.route.js" type="text/javascript"></script>...

Call modal window from factory method on ng-click

javascript,angularjs,angular-services
I Have a problem calling modal window from factory method on ng-click action. I've tried 2 ways to call: $scope.AddItem = function ($scope) {ModalWindowService.openAddItemDialog($scope)}; in this way, just nothing happens on click. but if i check with alert() it works. $scope.AddItem = function () {alert('here');}; And if i do this:...

AngularJS watcher in constructor not fired while testing

angularjs,jasmine,angular-services,angularjs-watch
I created a factory which returns a constructor. The constructor includes a watcher looking at a property of the instance. When that property is changed in tests, the watcher is not fired. Outside of tests, it is fired. $rootScope.$apply doesn't seem to help. Here is a jsfiddle which contains a...

What are the differences between various uses of the term “services” in AngularJS?

angularjs,angular-services
I'm learning AngularJS and I'm seeing the term "service" used in three different places in the developer guide, and it's confusing me just a little bit. A. In the Angular developer guide, there's a section on services. It goes on to describe how to declare these using myModule.factory. B. In...

How should I use Angular services with factories for code reuse?

angularjs,factory,angular-services
I have some Angular Controllers where there's always the need to store an array of items, push elements to it and make it accessible. I thought then I could have a ServicesModule with a factory offering such service. So I got this service declaration: angular.module('ServicesModule',[]); angular.module('ServicesModule') .factory('newOrdersService', function($rootScope,$http) { var...

Preserve data AngularJS after refreshing

javascript,json,angularjs,angularjs-scope,angular-services
This is my situation: I have a web application, frontend written in AngularJS/HTML. For the moment, I have 2 views (2 different HTML - pages). I enter in the first view a serial number, which is passed on from the first controller to the controller of the 2nd view with...

AngularJS Update Service Variable Not Working

javascript,jquery,angularjs,angular-services
I have a list of images. One next to the other. I'm trying to capture which image was clicked, populate a modal div and show it. Markup <section class="portfolio-grid column small-12" ng-controller="PortfolioCtrl"> <ul class="small-block-grid-2 medium-block-grid-4"> <li ng-repeat="portfolio in portfolios"> <a ng-click="showModal($index)" title=""> <img ng-src="{{portfolio.thumb}}" alt=""> <div class="details"> <h4>{{portfolio.name}}</h4> </div> </a>...

Angular http returns $$state object

javascript,angularjs,angular-services
I have the following factory defined: angular.module("account").factory("users",["$http", function(a){ return { getUser: function(){ return a.get("/user/me").then(function(r){ return r.data; }); } }; } ]); And my controller: angular.module("test.controllers",["account"]) .controller("TestCtrl",["$scope","users", function(a,u){ a.user = u.getUser(); console.log(a.user); }]); Here's the console.log: d {$$state: Object, then: function, catch: function, finally: function} $$state: Object status: 1 value: Object...

How to move a http request and pass value to an AngularJS Service

angularjs,angular-services
I'm trying to move this http request to a service and because I'm new to AngularJS I'm having some real issues. Can you help me figure out how to move not only the http request but the viewValue over from the controller to the service? app.controller('poController', function poController($scope, $http, poService)...

Angular JS service dependency injection error

angularjs,angularjs-directive,angular-resource,angular-services
I am new to angular I have been trying to do asynchronous validation for username availability and I am getting "Cannot read property 'userName' of undefined" Here is my service code webAppServices.factory('services', ['$resource', function ($resource){ return{ users: $resource('http://localhost:8080/api/users',{},{ get:{method:'GET',isArray:true}, add:{method:'POST',isArray:false}, update:{method:'POST',isArray:false} }), userName:$resource('http://localhost:8080/api/users/check/:username',{},{ check:{method:'GET',isArray:false} }) };}]);...

how to wait until promise returns when using $resource

angularjs,angular-services,ngresource
I have a my controller with an activate method which waits for promises to return before going forward. function activate() { var promises = [getNodesGroup(), getNodes()]; common.activateController(promises, controllerId) .then(function () { log('Activated'); }); } Both of the promises had similar approach to get the data and that was using $http...

AngularJS Script to run after ng-repeat elements are loaded

angularjs,angularjs-directive,lazy-loading,ng-repeat,angular-services
I have a problem that I wasn't able to solve even looking to other posts on Stackoverflow. I'm making an App with AngularJS, and I would like to pick some links of images from my Json that stays on an apache2 server and swipe them like in a carousel. When...

Data not received by Controller from Service

javascript,angularjs,angular-services
My Service looks like app.service('SupportService', function ($http, $q, $timeout) { var data = {result:[]}; var getData = function() { $http.get('/rest/report/logs') .then(function (response) { data.result = response.data.result; console.log("received logs:" + JSON.stringify(response.data.result)); }); }; getData(); return { data: data.result }; }); and in my controller, I do var init = function ()...

Angular Promise Called Twice is Only Deferring to First Caller

angularjs,jsonp,deferred,angular-promise,angular-services
I'm working on a video series app that's set up like so: angular.module('videoSeries', ['ngAnimate', 'ui.router']) .config(config) .factory('episodes', episodesFactory) .controller('MainCtrl', MainCtrl) .controller('EpisodeCtrl', EpisodeCtrl); The factory 'episodes' loads a JSONP file that both controllers need to read from. In order to ensure that the controllers wait until the JSONP has successfully returned,...

How to observe or watch an input attribute change in an AngularJS Service?

javascript,jquery,angularjs,angularjs-service,angular-services
I have a radio button in my UI that has an ngModel and it enable/disable another input through ngDisabled. I want to observer/watch/trigger when an input element become enabled/disabled and it has to be done in an Angular Service. I simply can't find how to do that. Here is how...

Angular updating and clearing factory variables

angularjs,angularjs-scope,angular-routing,angular-services
I'm creating a single page app in which a user searches for a term, the result gets saved in a variable, and a new page is routed that displays the result. I have this functionality working, however I want the variable to be cleared when the user returns to the...

Angular dealing with incorrect cached data

javascript,angularjs,angular-services
Okay this might be a long post but please do not click away you may know a simple answer. The case: Lets say you have build an angular app where people log into the system do some operations and then might log out again. The application will collect data from...

Convert factory to service in Angularjs

angularjs,angularjs-service,angular-services
I'm using $resource to fetch a json from server which is working fine: topicServices.factory('Topic', ['$resource', function($resource){ return $resource('http://localhost/topic_api_response.json', {}, { fetchData: {method:'GET', isArray:true} }); }]); However, now I want to convert this factory to a service by doing something like this: topicServices.service('Topic', ['$resource', function($resource) { this.fetchData = function() { var...

Am I using this Angular factory wrong from controller?

angularjs,angular-services
I have a very simple factory: var tv3Services = angular.module('tv3Services',[]); tv3Services.factory('PanelService', [function(){ var _panel = ""; return { getPanel: function() {return _panel;}, changePanel: function(newPanel) {_panel = newPanel;} }; }]); And a controller which uses it: var tv3App = angular.module('tv3App', ['tv3Services']); tv3App.controller('AdminController', function($scope,PanelService) { $scope.panel = PanelService.getPanel(); $scope.changePanel = function(panel) {...

AngularJs Error: [$injector:unpr] while attempting to inject $httpProvider to a service

angularjs,angular-services
I'll try to keep it short. Basically I have this module... var mod1= angular.module("mod1", ['ngRoute', 'ngCookies']); then a service, an interceptor, a factory and a controller... mod1.service("sessionService", sessionService); mod1.factory("loginFactory", loginFactory); mod1.controller("accountController", accountController); The sessionService is in a separate file which basically exposes a few functions to session info in local...

Removing brackets around a string and placing contents into new object

javascript,regex,angularjs,angular-services,angular-filters
I have an angular service which returns an array with a number of objects inside. $scope.data: [ { date: "03/12/2014", name: "mr blue", title: "math teacher (Germany)" }, { date: "04/02/2015", name: "mrs yellow", title: "chemistry teacher (Spain)" }, ] You can see from the title field it contains a...

Angular service will not inject into contorller

javascript,angularjs,ionic-framework,angular-services,angularjs-factory
I've made a handful of Angular applications in the past, but have never really made use of modules. I'm creating an Ionic app right now (which uses Angular) and for some reason I cannot get my services to inject into the controllers. controllers.js angular.module('myapp.controllers', []) .controller('StreamCtrl', ['$scope', 'StreamService', function($scope, StreamService)...

angularjs service calling $location.path is it against best practices?

javascript,angularjs,angular-ui-router,anti-patterns,angular-services
I am using a service in angularJS where I call $location.path inside one method in order to make a route change after a success or failure response from another service internally. Is this against Angular's best practices?

How do I define CRUD operations in a service (AngularJS)

javascript,angularjs,crud,angular-services
I have a problem with my CRUD operations in the service. When I click on Create Btn it is creating an object but it doesn't push the object in the table list. Ctrl (where's the table list): $scope.nameslist = CrudService.getAll(); Ctrl (for Modal dialog): $scope.createItem = function (newObj) { CrudService.create(newObj);...

ngOption default selected option

angularjs,angular-ngmodel,angular-services,angularjs-ng-options
How to have a default option in select box...I tried several options and do not get it I tried creating a scope on my contralador with the value of the object that comes from json: Like this: $scope.productSelect = $scope.item[0]; this my code: html: <select id="variant" class="form-control variant-select" ng-model="productSelect" ng-options="product...

angularjs override service setter and getter

javascript,angularjs,angular-services,angularjs-factory
I'm new to JS and angular, I have a currentUser service in angular... It should set the current user retrieved to the localStorage to persist it between page refreshes the service is defined as follows var currentUserSrvc = function() { // some logic on how to save the user to...

Angular service that compiles objects from two controllers to create 'Content Feed'

angularjs,angularjs-ng-repeat,angular-services
I have two controllers for two sections of a potential website: 'Articles' and 'Resources'. I'd like to have a 'Feed' section that combines the objects from these two sections and lists them all. I'm using ng-repeat for all of this. I'm not sure whether a service or a factory is...