angularjs-service

Use $routeParams in service

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 21:13:48
问题 I'm trying to append a query parameter to all API calls which I fetch from the URL. factory('Post', function ($resource, $routeParams) { return $resource('api/posts', {customer_id: $routeParams.customerId}); }) This works the first time the Post service is used but the second time it's injected it has already been initialized and is using the first customer ID even though the URL has changed. How do I make this work as intended? 回答1: Because services are singletons. That's why you get that

Restangular api calls to external getting 'No Access Allowed'

你说的曾经没有我的故事 提交于 2019-11-29 16:59:00
Following Restangular's documentation and includes an example of this: // GET to http://www.google.com/ You set the URL in this case Restangular.allUrl('googlers', 'http://www.google.com/').getList(); I am trying to do the same for angel.co $scope.trial = Restangular.allUrl('googlers', 'https://api.angel.co/1/users/267770').getList(); And keep getting error XMLHttpRequest cannot load https://api.angel.co/1/users/267770. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. Any solutions? The server (api

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

假如想象 提交于 2019-11-29 16:45:20
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.cityName}} I have around the page. module.service('cityFactory', function () { return { originCity: {

Using Angularjs $http in browser console

我是研究僧i 提交于 2019-11-29 16:37:16
问题 I've testing AngularJS services in browser console during development for quick verification. The way I inject a service into console is as describe in this question or var $inj = angular.injector(['myApp']); var serv = $inj.get('myService'); serv.doSomething(); That was working perfectly with AngularJS 1.0.7. However, after upgrading to 1.1.5, it doesn't work anymore for services that uses $http service, that no xhr will be sent. I've tested injecting $http directly, it also doesn't work.

Issue with manual bootstrapping and overriding angular services in config

安稳与你 提交于 2019-11-29 15:49:56
问题 I am trying to get my angular application work in live mode as well as in a prototype mode just by overriding the services. As a part of this when the prototype mode is turned on in the config, i halt the bootstrap process, load mock services (js) files and resume bootstrapping. Here is a simplified list of source code for preparing the demo :- App.js Just my app, which for simulation calls the service and display the result. It required StubApp as well whose provide this is going to use to

Router resolve will not inject into controller

自作多情 提交于 2019-11-29 15:10:47
问题 I have tried everything to get ui-router's resolve to pass it's value to the given controller–AppCtrl. I am using dependency injection with $inject , and that seems to cause the issues. What am I missing? Routing $stateProvider.state('app.index', { url: '/me', templateUrl: '/includes/app/me.jade', controller: 'AppCtrl', controllerAs: 'vm', resolve: { auser: ['User', function(User) { return User.getUser().then(function(user) { return user; }); }], } }); Controller appControllers.controller(

angularjs $http.get to get json not working in the service layer

纵饮孤独 提交于 2019-11-29 12:20:02
问题 I am developing an angularjs app as a part of my angularjs learning. I have controllers and from there I am calling service layers. leagueManager.service("teamsService", function($http){ var teams = {}; $http.get('data/teams.json').then(function(data) { teams = data; }); this.getTeams = function(){ return teams; }; }); I noticed that because of the asynchronous nature of $http.get.then stuff, the data is not retrieved immediately and hence I would not get the "teams" when I would call

Call controller function from service in angularjs

谁都会走 提交于 2019-11-29 12:17:16
问题 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

angularjs: using service to communicate between controllers

不问归期 提交于 2019-11-29 12:05:17
I have a service that is injected in my controllers. The service defines a number of functions. Now I would like to add a variable to that service that would hold the selectedItem in the application. I've done it this way: angular.module('myservices', []). factory('serviceA', function () { var serviceA= { selectedItem: selectedItem, ... more functions here }; return serviceA; var selectedItem; ... functions go here }); In one of my controllers I set the selected item: serviceA.selectedItem = someItem; and in another controller the view references the selected item like this: <span>{{serviceA

Initialize $scope variables for multiple controllers - AngularJS

╄→尐↘猪︶ㄣ 提交于 2019-11-29 08:27:22
I have 3 controllers that do similar tasks: PastController queries an API for past system outages. CurrentController queries an API for current system outages FutureController queries an API for future system outages They are each unique (despite their similar functions). However, they all begin by defining the same $scope variables: app.controller("PastController", function ($scope) { $scope.Outages = ""; $scope.loading = 0; $scope.nothing = 0; $scope.error = 0; //--- code continues ---// }); app.controller("CurrentController", function ($scope) { $scope.Outages = ""; $scope.loading = 0;