angularjs-service

How to create this global constant to be shared among controllers in Angularjs?

限于喜欢 提交于 2019-11-27 07:31:16
问题 Suppose I want to make this a variable a constant to be shared among controllers in Angularjs; $webroot = "localhost/webroot/app" After some investigation, it seems services are the way to do. But what is the best way? Do I use a factory, service, value or something else? The services.js from angularjs-master-seed is below; angular.module('myApp.services', []).value('version', '0.1'); How do I modify it to have a constant $webroot that is sharable among controllers? Can I do the following?

How can I test an AngularJS service from the console?

梦想与她 提交于 2019-11-27 05:43:16
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? 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,directives and other services. So what you need to do to get a service is to get the injector of AngularJS first (the

Angular Service Definition: service or factory

…衆ロ難τιáo~ 提交于 2019-11-27 04:47:32
问题 I am an angular newbie, I am building an application, one thing really puzzling me is there are couple of ways of defining a service, and I read more from this link: How to define service then it seems there is no big difference among the ways of defining a service. but I just noticed one difference which I think is different: see this service I get from here http://jsfiddle.net/2by3X/5/ var app = angular.module('myApp', []); app.service('test', function($timeout, $q) { var self = this; this

How to close Angular UI Modal from anywhere

和自甴很熟 提交于 2019-11-27 04:17:23
问题 I am using the Angular UI bootstrap modal dialog and create it within a service: myApp.factory('ModalService', ['$modal', function($modal) { return { trigger: function(template) { $modal.open({ templateUrl: template, size: 'lg', controller: function($scope, $modalInstance) { $scope.ok = function() { $modalInstance.close($scope.selected.item); }; $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; } }); }, close: function() { // this should close all modal instances } }; }]); How

Is CacheFactory in angularjs a singleton?

笑着哭i 提交于 2019-11-27 02:31:29
问题 I've created a service using the CacheFactory. I was expecting it to be a singleton. I inject it into my controller and it works fine within the scope of the controller. But once I go to a different page with a different scope, I don't seem to have the values in the cache that I stored in the same controller in a different scope. Shouldn't the behavior of the CacheFactory be a singleton where I have those same cached objects everywhere I inject the CacheService? This is my service as an

Can someone provide a use case for the $controller service in AngularJS?

余生长醉 提交于 2019-11-27 01:39:09
问题 Angularjs docs give the usage of $controller service as: $controller(constructor, locals); Can anyone focus some light on these 2 points: When to use $controller service. Please provide some use case. Details about 'locals' parameter passed to it. 回答1: You can create common functions which are to be executed on $scope into one controller may be named 'CommonCtrl' . angular.module('app',[]).controller('CommonCtrl', ['$scope', function($scope){ var self = this; $scope.stuff1 = function(){ }

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

你说的曾经没有我的故事 提交于 2019-11-27 00:22:58
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 access to some angularJS services and the scope of the controller that open that new window. Is this

AngularJS : The correct way of binding to a service properties

会有一股神秘感。 提交于 2019-11-26 21:14:01
I’m looking for the best practice of how to bind to a service property in AngularJS. I have worked through multiple examples to understand how to bind to properties in a service that is created using AngularJS. Below I have two examples of how to bind to properties in a service; they both work. The first example uses basic bindings and the second example used $scope.$watch to bind to the service properties Are either of these example preferred when binding to properties in a service or is there another option that I’m not aware of that would be recommended? The premise of these examples is

Should services expose their asynchronicity?

試著忘記壹切 提交于 2019-11-26 20:13:52
问题 I'm writing a service that will retrieve data asynchronously ($http or $resource). I can hide the fact that it is asynchronous by returning an array that will initially be empty, but that will eventually get populated: .factory('NewsfeedService1', ['$http', function($http) { var posts = []; var server_queried = false; return { posts: function() { if(!server_queried) { $http.get('json1.txt').success( function(data) { server_queried = true; angular.copy(data, posts); }); } return posts; } }; }]

Creating common controller functions

馋奶兔 提交于 2019-11-26 19:53:01
How do I create some sort of utils bundle that would be accessible from all my controllers? I have this route code in my main module: 'use strict'; angular.module('lpConnect', []). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/home', {template: 'views/home.html', controller: HomeCtrl}). when('/admin', {template: 'views/admin.html', controller: AdminCtrl}). when('/connect', {template: 'views/fb_connect.html', controller: MainAppCtrl}). otherwise({redirectTo: '/connect'}); }]); I want a function that can be common to HomeCtrl , AdminCtrl and MainAppCtrl . How should