What is AngularJS way to create global keyboard shortcuts?

I would say a more proper way (or “Angular way”) would be to add it to a directive. Here’s a simple one to get you going (just add keypress-events attribute to <body>): angular.module(‘myDirectives’, []).directive(‘keypressEvents’, [ ‘$document’, ‘$rootScope’, function($document, $rootScope) { return { restrict: ‘A’, link: function() { $document.bind(‘keypress’, function(e) { console.log(‘Got keypress:’, e.which); $rootScope.$broadcast(‘keypress’, e); … Read more

Angular.js: Is .value() the proper way to set app wide constant and how to retrieve it in a controller

Module.value(key, value) is used to inject an editable value, Module.constant(key, value) is used to inject a constant value The difference between the two isn’t so much that you “can’t edit a constant”, it’s more that you can’t intercept a constant with $provide and inject something else. // define a value app.value(‘myThing’, ‘weee’); // define a … Read more

How do I pass multiple attributes into an Angular.js attribute directive?

The directive can access any attribute that is defined on the same element, even if the directive itself is not the element. Template: <div example-directive example-number=”99″ example-function=”exampleCallback()”></div> Directive: app.directive(‘exampleDirective ‘, function () { return { restrict: ‘A’, // ‘A’ is the default, so you could remove this line scope: { callback : ‘&exampleFunction’, }, link: … Read more

Use underscore inside Angular controllers

When you include Underscore, it attaches itself to the window object, and so is available globally. So you can use it from Angular code as-is. You can also wrap it up in a service or a factory, if you’d like it to be injected: var underscore = angular.module(‘underscore’, []); underscore.factory(‘_’, [‘$window’, function($window) { return $window._; … Read more

How do I filter an array with AngularJS and use a property of the filtered object as the ng-model attribute?

You can use the “filter” filter in your controller to get all the “C” grades. Getting the first element of the result array will give you the title of the subject that has grade “C”. $scope.gradeC = $filter(‘filter’)($scope.results.subjects, {grade: ‘C’})[0]; http://jsbin.com/ewitun/1/edit The same with plain ES6: $scope.gradeC = $scope.results.subjects.filter((subject) => subject.grade === ‘C’)[0]

How npm start runs a server on port 8000

We have a react application and our development machines are both mac and pc. The start command doesn’t work for PC so here is how we got around it: “start”: “PORT=3001 react-scripts start”, “start-pc”: “set PORT=3001&& react-scripts start”, On my mac: npm start On my pc: npm run start-pc

Angularjs ui-router abstract state with resolve

Solution here is to distinguish the names of the resolved data. Check this answer: angular ui-route state parent and resolve (nested resolves) And its plunker So in our case we would need this change in parent resolve: { dataParent: [‘$stateParams’, ‘ProfileService’, function ($stateParams, ProfileService) { var username = $stateParams.username; return ProfileService.getProfile(username); }] } and this … Read more