JavaScript: Remove duplicates of objects sharing same property value

This function removes duplicate values from an array by returning a new one. function removeDuplicatesBy(keyFn, array) { var mySet = new Set(); return array.filter(function(x) { var key = keyFn(x), isNew = !mySet.has(key); if (isNew) mySet.add(key); return isNew; }); } var values = [{color: “red”}, {color: “blue”}, {color: “red”, number: 2}]; var withoutDuplicates = removeDuplicatesBy(x => … Read more

Mocking and Stubbing with protractor

This blog post discusses advance usage scenarios for Protractor. In particular it covers the the little know addMockModule() method of the Protractor browser object. The method allows you to create angular modules in Protractor (i.e. mocks or stubs of your API module) and upload them to the browser to replace the real implementation within the … Read more

How do i use $on in a service in angular?

after experimenting a fair bit it turns out that getting events to the service can be done with minimal code. sample service code follows in case anyone else runs into this. The sample saves and restores the service model to local storage when it gets the respective broadcasts app.factory(‘userService’, [‘$rootScope’, function ($rootScope) { var service … Read more

$http request doesn’t send cookies cross-domain in angular CORS

Have you seen this? Communication between AngularJS and a Jersey Webservice which are on a different domain. Can’t access correct session Try passing a config object to $http that specifies withCredentials, that should work in all versions. $http({withCredentials: true, …}).get(…) And the discussion here:

Angular JS POST request not sending JSON data

If you are serializing your data object, it will not be a proper json object. Take what you have, and just wrap the data object in a JSON.stringify(). $http({ url: ‘/user_to_itsr’, method: “POST”, data: JSON.stringify({application:app, from:d1, to:d2}), headers: {‘Content-Type’: ‘application/json’} }).success(function (data, status, headers, config) { $scope.users = data.users; // assign $scope.persons here as promise … Read more

How to generates dynamically ng-model=”my_{{$index}}” with ng-repeat in AngularJS?

Does it solve your problem? function MainCtrl($scope) { $scope.queryList = [ { name: ‘Check Users’, fields: [ “Name”, “Id”] }, { name: ‘Audit Report’, fields: [] }, { name: ‘Bounce Back Report’, fields: [ “Date”] } ]; $scope.models = {}; $scope.$watch(‘selectedQuery’, function (newVal, oldVal) { if ($scope.selectedQuery) { $scope.parameters = $scope.selectedQuery.fields; } }); } And … Read more

What is ‘cheaper’ performance-wise $broadcast or $watch

$watch() is doing dirt-checking: the function makes a comparison each digest cycle. On the other hand, $broadcast() propagates an event only when there is one. Naturally, $broadcast() is cheaper than $watch(). But did you really have to worry about performance here? One primitive comparison by cycle is nothing. However, conceptually, $watch() is clearly what you … Read more