FAQ Database Discussion Community


How to make a service object that doesn't share its values with controllers that consume it

angularjs,angularjs-service,angularjs-factory,angularjs-provider
Let's suppose I create a factory, service, or provider object like so myApp.factory('myService', function() { return { counter: 0, increment: function() { console.log('counter: ' + (this.counter++)) } }; }); And suppose I have a controller that depends on it myApp.controller('myCtrl', function(myService) { $scope.incr = function() { myService.increment(); }; } And...

AngularJS $httpProvider Error

angularjs,gulp,angularjs-service,angularjs-factory,angularjs-http
I'm writing a web application in AngularJS with Django API endpoints. When I want to perform a JSON call to this endpoints, I want to catch possible errors (such as 404 not found error). For this, I'm using $httpProvider. But when I run my application (using gulp), in the console,...

How to make a prototype out of 2 identical controllers in angularjs?

angularjs,dependency-injection,angularjs-service,angularjs-factory,angularjs-provider
In my app I have 2 almost identical controllers. A lot of functions are the same, so I'd like to prototype them. This is Controller #1: c2gcontroller.js angular.module('c2gyoApp') .controller('C2gCtrl', function($scope) { // some unique stuff $scope.feeDay = 59; ... // the identical functions $scope.getMinutes = function(minutes) { var duration =...

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...

AngularJs factory function undefined in controller

angularjs,asynchronous,angularjs-service,angularjs-controller,angularjs-factory
I used to make it work the exact same way before, and it's driving me crazy. I want to perform a $http GET call into a factory and then get back the result into the controller, to be processed. The factory (don't pay attention to the madness of the request...

angular js-factory service's flow between the calls

javascript,angularjs,angularjs-factory
I am new to angularjs and so i was going through the basic examples i found online initially just to understand the working and concepts used. When i encountered the concept of "factory service creation" (which is a way to expose data from server to views in and angularjs), i...

AngularJS testing $resource - flush giving error

javascript,angularjs,unit-testing,karma-jasmine,angularjs-factory
I have a factory angular.module('RepServices', [ 'ngResource' ]).factory('Rep', function($resource) { return $resource('rep.do', {}, { get : { method : 'GET', params : { action : "fetchRep" }, isArray : false, responseType : "text" } }); }); and have created a test describe('RepService test', function () { var httpBackend; var repService;...

Angular Js : Button click to get data from two controllers and showing it in third

angularjs,angularjs-scope,angularjs-controller,angularjs-factory
I am new to AngularJS. i am having a following functionality where i am having a FirstName field and LastName Field(in two Different Controllers) and then on the click of a button i want to show FullName(First Name + Last Name) in a third Controller . Please Suggest me how...

Update Angular ion-view in another controller

angularjs,angularjs-controller,angularjs-factory
Im new here on stackoverflow and new to AngularJS. Im trying to build a Ionic app, and here is my problem. I would like to change the date in one template(date.html), using Controller: DateCtrl, and change the GEO location in another template(geo.html), using Controller: GEOCtrl. And when I change the...

AngularJS $resource class vs. instance methods

angularjs,angularjs-resource,angularjs-factory
I define a factory for "Dashboard": myApp.factory('Dashboard', ['$resource', function($resource) { return $resource("/dashboards/:id", { id: "@id" }, { 'create': { method: 'POST' }, 'index': { method: 'GET', isArray: true }, 'show': { method: 'GET', isArray: false }, 'update': { method: 'PUT' }, 'destroy': { method: 'DELETE' } } ); }]); In...

How to send updated object from Service into Directive Controller function the Angular way?

javascript,angularjs,angularjs-directive,angularjs-service,angularjs-factory
I know I'm missing the AngularJS way to do this correctly. On mouseover in my tagsPanel's view I capture the tag object and first send it into my tagsPanel controller: tagsPanel.html <ul> <li ng-repeat="(k, m) in tags" ng-mouseover="hoverTag(m)" ng-mouseleave="leaveTag(m)"> <div class="tag">{{m.term}}</div> <tags-hover ng-model="m"></tags-hover> </li> </ul> tagsPanel Controller: vs.hoverTag = function(tagObj)...

Can't seem to inject angular-jwt into factory

javascript,angularjs,dependency-injection,jwt,angularjs-factory
I am trying to inject angular-jwt into a factory for use in auth functions, but I keep getting the error `Error: [ng:areq] Argument 'fn' is not a function, got string http://errors.angularjs.org/1.3.15/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20string return new ErrorConstructor(message);` Here's the code: webapp.factory('Auth', ['angular-jwt'], function($http, API_URL, $window, $location, jwtHelper ) { Also the learning curve...

AngularJS Factory JSON Array with HTTP GET

json,angularjs,angularjs-service,angularjs-factory
I'm developing my first AngularJS app using the Google Docs API to pass it JSON data. This is an example of the factory I'm using: app.factory('Data', ['$http', 'apiKeys', function($http, apiKeys){ var googleDocs = 'https://spreadsheets.google.com/feeds/list/'; return { news:function () { return $http.get(googleDocs + apiKeys.googleDoc +'/1/public/values?alt=json', {cache: true}); }, updates:function () {...

Sync directive data with factory value updated by an AJAX call

javascript,ajax,angularjs,angularjs-directive,angularjs-factory
I've got two simple directives, let's call them blue and red. What I need is for them to work in such a way that when I click the button in the red directive, the value in blue gets automatically updated. And vice versa. It's fairly simple to do using a...

Angularjs List/Detail Edit

angularjs,angularjs-scope,angularjs-factory
I am writing a small AngularJS/Ionic/Cordova application where I have a list of contacts. When the user taps on a contact, I navigate to a new page where details about the contact are shown (first name, last name, phone number). Here, the user can update details about the contact, or...

Bind factory data to angularjs controller

angularjs,angularjs-scope,angularjs-factory
How do I bind the array (conversations) to ng-repeat within the controller below. I've tried binding it in a function, the intent is for this to run when the controller loads up and then bind the data to the view. Any help appreciated. .factory('UserMessages', function ($q, $http) { var conversations...

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)...

Why does using $compile make factory execute multiple times

javascript,angularjs,angularjs-factory,angularjs-compile
In my code i need to compile html returned from another api from inside a javascript callback function. The following is a simplified version of my code. I am using a factory method which recomplies any element using $compile and $rootScope. The strange thing about this setup is that the...

Initialize Angular Service (factory) when application start

javascript,angularjs,angularjs-service,angularjs-factory
In my Angular application I adding tracing functionality, which should work as separate plugin, so if script included into HTML Angular should create service and initialize (run) it. Plugin is service because it has mandatory dependency on $rootScope. For implementation I select to use Angular factory, like (code is in...

Parse query then state change after resolve

angularjs,parse.com,angularjs-factory,angularjs-ui-router,angularjs-promise
I have a factory defined to make a parse call that resolves upon completion. .factory('currentRestaurantService', ['$rootScope', '$q', function($scope, $q) { return { getCurrentRestaurant: function() { var defer = $q.defer(); // Create a deferring object $scope.currentUser = Parse.User.current(); // Get restaurant for user var restaurantObject = Parse.Object.extend("Restaurant"); var query = new...

AngularJS: Controller inheritance using call (or apply)

angularjs,angularjs-controller,angularjs-factory
This is something I discovered when trying to make my controllers more reusable. app.factory('BaseController', function(myService) { return function(variable) { this.variable = variable; this.method = function() { myService.doSomething(variable); }; }; }) .controller("ChildController1", function($scope, BaseController) { BaseController.call($scope, "variable1"); }) .controller("ChildController2", function($scope, BaseController) { BaseController.call($scope, "variable2"); }); And now I can do something...

How to push objects into array inside of a JavaScript Prototype?

javascript,angularjs,prototype,angularjs-service,angularjs-factory
I have an Angular Factory whose job is to save special objects and retrieve them later. (Used to save the users workflow when he switches views). Basically I need to save objects with a name, as well as an array of tags. My problem is with the saving the tags...

access angularjs deferred promise from new template

angularjs,angularjs-service,angular-promise,angularjs-factory
I'm new to angular and LOVE it...coming from a 10 yr PHP background there is a lot of new terms for me. Here is what I would like to-do, not sure if it's possible, if yes, not sure how. I have created a list of properties and that part works...

Submitting Form in AngularJS and MVC 4

angularjs,asp.net-mvc-4,angularjs-controller,angularjs-factory
I am new to AngulaJS, and I am trying to do as many examples as I can. I found an example online that works pretty well except for the registration part. I tried to register a user; it did not submit the form, and there was not an error message/exception....

Angular - TypeError: XX is not a function

javascript,angularjs,angularjs-scope,angularjs-service,angularjs-factory
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: "...

multiple returned keys json

javascript,angularjs,ionic-framework,angularjs-factory,angularjs-http
I have a joomla site running k2 and pulling info from it by adding format=json to the end of the string: https://www.example.com/posts?format=json which outputs something like this: { site: { url: "https://www.example.com", name: "mySite" }, category: { id: "67", name: "Gauteng", alias: "gauteng", link: "/tna/provincial/gauteng.html", parent: "66", extraFieldsGroup: "0", image:...

How to call a factory method dynamically coming from variable?

javascript,angularjs,angularjs-factory,dynamic-variables
I have a service which will return the name of factory. I already injected all the factories into controller. I need to use the variable to call the method inside that factory. I know i can use if(var == 'factoryname') { factoryname.method() } but i don't want those if conditions...

angular directly send $scope parameter from view to service or factory

angularjs,angularjs-scope,angularjs-factory
I have a factory and need to call factory from view. i want to call factory with two parameters. Is it possible to send $scope from template? Because, i am using same factory for multiple places. <input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay($scope, accno)" /> controller, $scope.myservice= getAllDetailsService; In service, tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter',...

Angular - Organise controller, factory and “class”

angularjs,angularjs-scope,angularjs-factory
I would like to understand how to have a nice organisation in my angular project. [see code below] Does it makes sense to have the getFireList function into the Factory ? Or should i put it into the controller ? Does the "class" Fire makes sense ? Should i remove...

Use 1 method for all the function short down to one function in angularjs

javascript,angularjs,angularjs-scope,angularjs-controller,angularjs-factory
Hello i am trying to create an angular app. So here is one function which i am using very often in most of my controllers. Can i make one function or one method so can i can use this function everywhere just like we do in PHP. Here is the...