Skip to content

Instantly share code, notes, and snippets.

View jens1101's full-sized avatar

Jens Tschirpig jens1101

View GitHub Profile
@jens1101
jens1101 / component.js
Created October 5, 2017 07:29
Optional Angular bindings
var app = angular.module('testModulePleaseIgnore', [])
app.component('legalReferences', {
bindings: {
foo: '&?',
bar: '<?',
baz: '=?',
qux: '@?'
},
controller: function() {
@jens1101
jens1101 / bootstrap-select-app.js
Last active May 22, 2018 11:38
An angular component wrapper for bootstrap select.
var app = angular.module('app', [])
app.component('selectPicker', {
template: '<div ng-transclude ng-show="$ctrl.show"></div>',
require: {
ngModel: '^ngModel'
},
bindings: {
options: '<',
disable: '<'
@jens1101
jens1101 / Browser location handling.js
Last active October 9, 2020 21:07
The correct way to handle the browser location in JS
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Location
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/location
// Navigate to a new page.
window.location.assign('http://www.mozilla.org')
// Force reloading the current page from the server
// **Note:** If the parameter is not set or set to false then the browser may
// reload from cache
window.location.reload(true)
A; B    Run A and then B, regardless of success of A
A && B  Run B if A succeeded
A || B  Run B if A failed
A &     Run A in background.
@jens1101
jens1101 / require-ng-model.js
Created February 23, 2017 10:32
ngModel Cheatsheet. How to interact with ngModel via JavaScript in your own Angular components
var app = angular.module('example', []);
app.component('foo', {
require: {
theModel: '^ngModel'
},
template: '[...]',
controller: FooCtrl,
bindings: {
options: '<'
@jens1101
jens1101 / script.js
Last active January 14, 2023 08:44
Regex remove trailing slashes from a URL
/**
* @param {string} url
*/
function removeTrailingSlashes(url) {
return url.replace(/\/+$/, ''); //Removes one or more trailing slashes from URL
}
@jens1101
jens1101 / ngshow-nghide-use.md
Created January 25, 2017 11:53
Should I use `ng-show` or `ng-hide`?

Often times the choice between both directives seems trivial, because you can achieve the same effect either way. However both have interesting default behaviours that you can use to you advantage.

  • ng-show will hide the element it is on by default, unless the condition in it evaluates to true.
  • ng-hide will show the element it is on by default, unless the condition in it evaluates to true.

This is most useful when your controller is doing AJAX calls or something else that's asynchronous. Your variables may still be undefined until the AJAX call returns.

Example

foo.controller.js

$ctrl.foos;
@jens1101
jens1101 / Node.ts
Created December 3, 2016 20:08
How to store a class reference in Typescript
export class Node {
//...
}
export class UnidirectionalNode extends Node {
private next: Node;
//...
}
@jens1101
jens1101 / script.js
Created November 29, 2016 13:27
Load an HTML file as template in jQuery
$.get('/html/template01.html').done(function(html) {
var template = $(html); //Create DOM element
//Do stuff with the new DOM element
});
@jens1101
jens1101 / index.html
Created November 23, 2016 10:02
List item with a primary action and zero or more secondary actions. All items align vertically central. This is especially useful if you want to assign a click action to the primary content, or if you want to style the secondary items differently.
<div class="item-with-actions">
<i class="primary">
This is the primary content of the item
</i>
<div>
<button>
Edit
</button>
</div>
<div>