AngularJS – why is $apply required to properly resolve a $q promise?

In AngularJS the results of resolve() are propagated asynchronously, inside a $digest cycle, not immediately. This means that callbacks registered with then() will only be called (later) when a digest cycle occurs. In your code, nothing is causing Angular to enter a digest cycle, so the then() callback is never called. Calling $apply() is one … Read more

Angular factory returning a promise

$http itself return promise you don’t need to bind it inside the $q this is not a good practice and considered as Anti Pattern. Use:- app.factory(‘settings’, [‘$rootScope’, ‘$http’, ‘$q’, function($rootScope, $http) { return $http.get(‘/api/public/settings/get’) }]); app.controller(‘SomeCtrl’, [‘settings’,$scope, function(settings,$scope) { settings.then(function(result){ $scope.settings=result.data; }); }]); Your way can be done as :- app.factory(‘settings’, [‘$rootScope’, ‘$http’, ‘$q’, function($rootScope, … Read more

Multiple chained deferred functions using q in AngularJS stop returning data

Excerpt taken from the official doc on $q: then(successCallback, errorCallback) – regardless of when the promise was or will be resolved or rejected calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument the result or rejection reason. This method returns … Read more

Adding http headers to window.location.href in Angular app

When you use $window.location.href the browser is making the HTTP request and not your JavaScript code. Therefore, you cannot add a custom header like Authorization with your token value. You could add a cookie via JavaScript and put your auth token there. The cookies will automatically be sent from the browser. However, you will want … Read more

How to programmatically create URLs with AngularJS

Starting from v1.4 you can use $httpParamSerializer for that: angular.module(‘util’).factory(‘urlBuilder’, function($httpParamSerializer) { function buildUrl(url, params) { var serializedParams = $httpParamSerializer(params); if (serializedParams.length > 0) { url += ((url.indexOf(‘?’) === -1) ? ‘?’ : ‘&’) + serializedParams; } return url; } return buildUrl; }); Usage: To produce http://url?param1=value1&param2=value2_1&param2=value2_2 call it with: urlBuilder(‘http://url’, { param1: ‘value1’, param2: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)