Angularjs: a Service that serves multiple $resource urls / data sources?

You can update service to return a hash of resources, not a single one: angular.module(‘myApp.services’, [‘ngResource’]). factory(“geoProvider”, function($resource) { return { states: $resource(‘../data/states.json’, {}, { query: { method: ‘GET’, params: {}, isArray: false } }), countries: $resource(‘../data/countries.json’, {}, { query: { method: ‘GET’, params: {}, isArray: false } }) }; }); You will be able … Read more

Hour difference between two times(HH:MM:SS a)in momentjs

Try code below // start time and end time var startTime = moment(’12:16:59 am’, ‘HH:mm:ss a’); var endTime = moment(’06:12:07 pm’, ‘HH:mm:ss a’); // calculate total duration var duration = moment.duration(endTime.diff(startTime)); // duration in hours var hours = parseInt(duration.asHours()); // duration in minutes var minutes = parseInt(duration.asMinutes()) % 60; alert(hours + ‘ hour and ‘ … Read more

Setting application wide HTTP headers in AngularJS

You can use default headers for angular 1.0.x: $http.defaults.headers.common[‘Authentication’] = ‘authentication’; or request interceptor for angular 1.1.x+: myapp.factory(‘httpRequestInterceptor’, function () { return { request: function (config) { // use this to destroying other existing headers config.headers = {‘Authentication’:’authentication’} // use this to prevent destroying other existing headers // config.headers[‘Authorization’] = ‘authentication’; return config; } }; … Read more

Retain scroll position on route change in AngularJS?

I have a fiddle here that shows how to restore scroll position in the list view after a detail view; not encapsulated in a directive yet, working on that… http://jsfiddle.net/BkXyQ/6/ $scope.scrollPos = {}; // scroll position of each view $(window).on(‘scroll’, function() { if ($scope.okSaveScroll) { // false between $routeChangeStart and $routeChangeSuccess $scope.scrollPos[$location.path()] = $(window).scrollTop(); //console.log($scope.scrollPos); … Read more

WARNING: Tried to load angular more than once. Angular JS

In my case this was due to the following html code: <!doctype html> <html> <head> <meta charset=”utf-8″> <title>Testapp</title> </head> <body ng-app=”testApp”> <main ui-view> <script src=”https://stackoverflow.com/questions/23499853/bower_components/jquery/jquery.js”></script> <script src=”bower_components/angular/angular.js”></script> <script src=”bower_components/angular-ui-router/release/angular-ui-router.js”></script> <script src=”scripts/main.js”></script> </body> </html> As you can see, the <main> is not closed. This led to my variant of ‘WARNING: Tried to load angular more than … Read more

How to split a string with angularJS

You may want to wrap that functionality up into a filter, this way you don’t have to put the mySplit function in all of your controllers. For example angular.module(‘myModule’, []) .filter(‘split’, function() { return function(input, splitChar, splitIndex) { // do some bounds checking here to ensure it has that index return input.split(splitChar)[splitIndex]; } }); From … Read more

AngularJS $watch vs $watchCollection: which is better for performance?

$watch() will be triggered by: $scope.myArray = []; $scope.myArray = null; $scope.myArray = someOtherArray; $watchCollection() will be triggered by everything above AND: $scope.myArray.push({}); // add element $scope.myArray.splice(0, 1); // remove element $scope.myArray[0] = {}; // assign index to different value $watch(…, true) will be triggered by EVERYTHING above AND: $scope.myArray[0].someProperty = “someValue”; JUST ONE MORE … Read more

Dynamic class in Angular.js

You can simply assign a function as an expression and return proper class from there. Edit: there is also better solution for dynamic classes. Please see note below. Snippet from view: <div ng-class=”appliedClass(myObj)”>…</div> and in the controller: $scope.appliedClass = function(myObj) { if (myObj.someValue === “highPriority”) { return “special-css-class”; } else { return “default-class”; // Or … Read more