angularjs-service

How can I test an AngularJS service from the console?

倖福魔咒の 提交于 2019-11-26 10:03:58
问题 I have a service like: angular.module(\'app\').factory(\'ExampleService\', function(){ this.f1 = function(world){ return \'Hello \'+world; } return this; }) I would like to test it from the JavaScript console and call the function f1() of the service. How can I do that? 回答1: TLDR: In one line the command you are looking for: angular.element(document.body).injector().get('serviceName') Deep dive AngularJS uses Dependency Injection (DI) to inject services/factories into your components

AngularJS: open a new browser window, yet still retain scope and controller, and services

a 夏天 提交于 2019-11-26 09:21:29
问题 I\'m writing an angularJS app. In this particular controller, I open a new browser window through the $window.open service. But in the new window, all the $scope variables are lost. I tried to use window.parent but this doesn\'t work. In fact in the new browser window all the app services, controllers, or scopes are not in effect at all, is this true? Is there a way to open a new browser window yet still makes the new window belongs to the same angular app in the old window? I need to have

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

怎甘沉沦 提交于 2019-11-26 08:57:57
问题 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

Cordova + Angularjs + Device Ready

*爱你&永不变心* 提交于 2019-11-26 05:16:31
问题 I am developing a mobile application using Cordova and AngularJS. How do I restrict bootstrapping of AngluarJS before Cordova device ready. Basically I don\'t want to use any of AngularJS controllers before device ready. 回答1: Manually bootstrap your Angular app: Remove your ng-app attribute from your HTML code, so Angular doesn't start itself. Add something like this to you JavaScript code: document.addEventListener("deviceready", function() { // retrieve the DOM element that had the ng-app

AngularJS : When to use service instead of factory

╄→гoц情女王★ 提交于 2019-11-26 04:57:16
问题 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

AngularJS : Factory and Service? [duplicate]

北城余情 提交于 2019-11-26 04:02:49
问题 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

Injecting a mock into an AngularJS service

情到浓时终转凉″ 提交于 2019-11-26 02:27:28
问题 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:

Is this a “Deferred Antipattern”?

泄露秘密 提交于 2019-11-25 21:43:59
问题 I\'m finding it hard to understand the \"deferred antipattern\". I think I understand it in principal but I haven\'t seen a super simple example of what a service, with a differed promise and one with antipattern, so I figured I\'d try and make my own but seeing as how I\'m not super in the know about it I\'d get some clarification first. I have the below in a factory (SomeFactory): //url = \'data.json\'; return { getData: function(){ var deferred = $q.defer(); $http.get(destinationFactory

AngularJS: Service vs provider vs factory

半腔热情 提交于 2019-11-25 21:36:02
问题 What are the differences between a Service , Provider and Factory in AngularJS? 回答1: From the AngularJS mailing list I got an amazing thread that explains service vs factory vs provider and their injection usage. Compiling the answers: Services Syntax: module.service( 'serviceName', function ); Result: When declaring serviceName as an injectable argument you will be provided with an instance of the function. In other words new FunctionYouPassedToService() . Factories Syntax: module.factory(