angularjs-service

Angularjs: Error: [ng:areq] Argument 'HomeController' is not a function, got undefined

谁说我不能喝 提交于 2019-11-26 19:40:29
This is my demo using angularjs, for creating a service file, and adding service to a controller. I have two problems with my demo: One is when I put <script src="HomeController.js"> before <script src="MyService.js"> I get this error, Error: [ng:areq] Argument 'HomeController' is not a function, got undefined The other is when I put <script src="MyService.js"> before <script src="HomeController.js"> I get the following error, Error: [$injector:unpr] Unknown provider: MyServiceProvider <- MyService My source: File Index.html : <!DOCTYPE html> <html > <head lang="en">…</head> <body ng-app=

Call angularjs service from simple js code

我的未来我决定 提交于 2019-11-26 19:24:35
问题 I have the following angularjs service: angular.module('app.main').factory('MyService', ["$http", function ($http) { return new function () { this.GetName = function () { return "MyName"; }; }; }]); How can I call GetName function from MyService from legacy js code? 回答1: Use angular.injector. Using your code you can do something like the following: angular.module('main.app', []).factory('MyService', ['$http', function ($http) { return new function () { this.GetName = function () { return

How to wait till the response comes from the $http request, in angularjs?

空扰寡人 提交于 2019-11-26 19:20:56
I am using some data which is from a RESTful service in multiple pages. So I am using angular factories for that. So, I required to get the data once from the server, and everytime I am getting the data with that defined service. Just like a global variables. Here is the sample: var myApp = angular.module('myservices', []); myApp.factory('myService', function($http) { $http({method:"GET", url:"/my/url"}).success(function(result){ return result; }); }); In my controller I am using this service as: function myFunction($scope, myService) { $scope.data = myService; console.log("data.name"+$scope

What does the underscores in _servicename_ mean in AngularJS tests?

ⅰ亾dé卋堺 提交于 2019-11-26 19:04:32
问题 In the following example test, the original provider name is APIEndpointProvider, but for injection and service instantiation the convention seems to be it has to be injected with underscores wrapping it. Why is that? 'use strict'; describe('Provider: APIEndpointProvider', function () { beforeEach(module('myApp.providers')); var APIEndpointProvider; beforeEach(inject(function(_APIEndpointProvider_) { APIEndpointProvider = _APIEndpointProvider_; })); it('should do something', function () {

Linking one controller to another to call service on ng-click

两盒软妹~` 提交于 2019-11-26 17:53:22
问题 I have two templates with respective controllers and service files. One template's(fleetListTemplate) controller(fleetListController) loads data from its service file(fleetService) and displays in its view(fleetListTemplate). When this happens, and I click on one of the loaded data from fleetService, I should link fleetListController to fleetSummaryController to get data from its service file (fleetSummaryService) and display in fleetSummaryTemplate view. Can someone please help me with the

AngularJS : When to use service instead of factory

梦想的初衷 提交于 2019-11-26 16:46:19
Please bear with me here. I know there are other answers such as: AngularJS: Service vs provider vs factory However I still can't figure out when you'd use service over factory. From what I can tell factory is commonly used to create "common" functions that can be called by multiple Controllers: Creating common controller functions The Angular docs seem to prefer factory over service. They even refer to "service" when they use factory which is even more confusing! http://docs.angularjs.org/guide/dev_guide.services.creating_services So when would one use service? Is there something that is only

How to enable cors request with angular.js-resource

£可爱£侵袭症+ 提交于 2019-11-26 16:45:46
问题 I have an angular.js application and i need to do CORS request. I want to define my rest services "the angular" using angular resources, described here: http://docs.angularjs.org/tutorial/step_11. But i haven't found a way to get this working. On google i found the following sample code: http://jsfiddle.net/ricardohbin/E3YEt/, but this seems not to work with angular-resources. this is my app.js 'use strict'; angular.module('corsClientAngularApp', ['helloServices']) .config(function (

AngularJS factory http returns empty

ⅰ亾dé卋堺 提交于 2019-11-26 15:48:42
问题 I'm trying AngularJS for the first time. I'm getting JSON data from a http-get request using a factory, but the object is returned empty, before the ajax-request is done. Factory: myDemo.factory('photosFactory', function($http) { var photos = []; var factory = {}; factory.getPhotos = function() { $http({ url: 'content/test_data.json', method: 'GET' }).success(function(data, status, headers, config) { photos = data; return photos; }); }; return factory; }); Controller: controllers.AppCtrl =

AngularJS : Factory and Service? [duplicate]

久未见 提交于 2019-11-26 14:49:59
This question already has an answer here: AngularJS: Service vs provider vs factory 30 answers EDIT Jan 2016: Since this still gets attention. Since asking this I've completed a few AngularJS projects, and for those I mostly used factory , built up an object and returned the object at the end. My statements below are still true, however. EDIT : I think I finally understand the main difference between the two, and I have a code example to demonstrate. I also think this question is different to the proposed duplicate. The duplicate says that service is not instantiable, but if you set it up as I

Injecting a mock into an AngularJS service

爱⌒轻易说出口 提交于 2019-11-26 11:32:36
I have an AngularJS service written and I would like to unit test it. angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']). factory('myService', function ($http, fooService, barService) { this.something = function() { // Do something with the injected services }; return this; }); My app.js file has these registered: angular .module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider'] ) I can test the DI is working as such: describe("Using the DI framework", function() { beforeEach(module('fooServiceProvider')); beforeEach(module(