angularjs-service

How to make controller wait for promise to resolve from angular service

情到浓时终转凉″ 提交于 2019-12-23 07:11:45
问题 I have a service that is making an AJAX request to the backend Service: function GetCompaniesService(options) { this.url = '/company'; this.Companies = undefined; this.CompaniesPromise = $http.get(this.url); } Controller: var CompaniesOb = new GetCompanies(); CompaniesOb.CompaniesPromise.then(function(data){ $scope.Companies = data; }); I want my service to handle the ".then" function instead of having to handle it in my controller, and I want to be able to have my controller act on that data

UI-Router will not resolve $http call

北城余情 提交于 2019-12-23 06:13:12
问题 I have a api that i want to resolve name for "bubble". This i would like to inject into controller - but problem is that i have tried different approaches and all just say...undefined and some injection problems... :( Code: .state('bubbles', { abstract: false, url: "/bubbles/:bubbleId/feed", controller: 'BubblesController', data: { authorizedRoles: [USER_ROLES.editor] }, resolve: { resolvedBubbleName: function ($http, $stateParams) { var url = 'https://XXXXXXX.net/api/Bubble/11111111-1111

AngularJS $location.path(path) not updating at first

不想你离开。 提交于 2019-12-23 01:37:33
问题 I have a piece of code where I call $location.path(name) and nothing seems to happens (at first). I can call console.log($location.path()) and it does show the new path -- but the view doesn't change. Chain of events (some asynchronous): I have two controllers (and matching views), MainCtrl and CpugraphCtrl, and a Service GetDataService and one external data API (Google). button in Main view calls function in MainCtrl that calls function in GetDataService that calls google client API gapi

Angularjs - Restangular call inside factory/service

本小妞迷上赌 提交于 2019-12-22 17:18:41
问题 I am using factory as follows: var appModule = angular.module('appModule', ['ngRoute','restangular']); appModule .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/home', { templateUrl: 'home.html', controller: 'ngHomeControl' }) .when('/contacts', { templateUrl: 'contacts.html', controller: 'ngContactControl' }); }]); appModule .factory('testFactory', function testFactory(Restangular) { return { getFriendList: function() { return Restangular .all('api/users

Share model between controllers

你。 提交于 2019-12-22 12:21:15
问题 I'm climbing my learning curve in angular.js and try to understand where to put everything. In this case I want to know if it is a best practice to use services to share the model between controllers. var app = angular.module('module', []) .factory('shared', ['$http', function ($http) { var factory = { "title" : "Shared Title 2" }; factory.action = function () { // do something with this.title; } return factory; }]) .controller('MainCtrl', ['$scope','shared', function($scope, shared) { $scope

AngularJS : How to use the $q promise feature in this situation to wait for data to be ready?

允我心安 提交于 2019-12-22 10:05:13
问题 I have a Controller which starts an API call in a Factory . Currently I start the call, then I have a function after that call to check the status of the Array . However I'm not sure how to force it to wait here while the data is being gathered, thus the need for some kind of $q implementation. As you can see in the screenshot below, vs.tickers returns an empty Object, or Array. Then finally the console.log in GetTickersFactory fires: 1st Controller if (root.tickerType === 'portfolio') { //

AngularJS: How to share scope functions and variables with other controllers

我怕爱的太早我们不能终老 提交于 2019-12-21 17:51:37
问题 I've multiple controllers in my application, where I have some duplicate code like: $scope.alert = null; $scope.addAlert = function (message) { $scope.alert = { type: 'danger', msg: message }; }; $scope.clearAlerts = function () { $scope.alert = null; }; What is the recommended way sharing these scope functions and variables in AngularJS? Using controller inheritance? 回答1: Create a one controller and then place common methods inside that controller scope. So that you can use that scope

How to set a timeout to abort an $http.get() inside a factory or service?

不问归期 提交于 2019-12-21 12:15:19
问题 I have the following method getData(url) in a my factory which uses $http.get(url) to get data from an URL angular .module('az-app') .factory('WebServiceFactory', function ($http, $q) { var WebServiceFactory = this; WebServiceFactory.getData = function (url) { var deferred = $q.defer(); $http.get(url) .then( function (response) { deferred.resolve({ data: response }); }, function (rejected) { deferred.reject({ data: rejected }); } ); //Promise to be returned return deferred.promise; } It works

can't get service instance from $injector.get()

走远了吗. 提交于 2019-12-20 11:55:16
问题 I define a customer service named "greeting", but can't get the instance from $injector.get('greeting'). It will throw such error: Unknown provider: greetingProvider <- greeting . So which is the right way to get it? Following is the code: var app = angular.module('myDI', []); app.config(function($provide){ $provide.provider('greeting', function(){ this.$get = function(){ return function(name) { console.log("Hello, " + name); }; }; }); }); var injector = angular.injector(); var greeting =

How do I prevent angular-ui modal from closing?

非 Y 不嫁゛ 提交于 2019-12-20 08:10:53
问题 I am using Angular UI $modal in my project http://angular-ui.github.io/bootstrap/#/modal I don't want user to close the modal by pressing on backdrop. I want a modal can only be closed by pressing close button which I have created. How do I prevent modal from closing ? 回答1: While you creating your modal you can specify its behavior: $modal.open({ // ... other options backdrop : 'static', keyboard : false }); 回答2: backdrop : 'static' Will work for 'click' events but still you can use "Esc" key