Skip to content

Instantly share code, notes, and snippets.

View EpokK's full-sized avatar

Richard EpokK

View GitHub Profile
@EpokK
EpokK / ngFocus.js
Created July 20, 2013 10:48
ngFocus
myApp.directive('ngFocus', function( $timeout ) {
return function( scope, elem, attrs ) {
scope.$watch(attrs.ngFocus, function( newval ) {
if ( newval ) {
$timeout(function() {
elem[0].focus();
}, 0, false);
}
});
};
@EpokK
EpokK / unwatch.js
Created July 23, 2013 15:56
Unwatch an expression: Sometimes you want to watch an expression only a few times, and then forget it. The $watch function returns a callback just for that. You just have to execute it back to destruct the watcher.
var watcher = $scope.$watch('data.counter', function(newValue, oldValue) {
iElement.css('width', 50 * newValue + 'px');
if (newValue >= 10) {
// when data.counter reaches 10, destruct the watcher.
watcher();
}
});
@EpokK
EpokK / index.html
Created July 27, 2013 10:19
Test ractive.js
<h1>Bye {{code}}</h1>
@EpokK
EpokK / diff.js
Created July 30, 2013 08:49
Difference between Service, Factory and Provider in AngularJS
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
};
});
//factory style, more involved but more sophisticated
@EpokK
EpokK / filterCut.js
Last active December 20, 2015 17:49
Limit the length of a string with a filter
/**
* Usage:
* {{some_text | cut:true:100:' ...'}}
* Options:
* - wordwise (boolean) - if true, cut only by words bounds,
* - max (integer) - max length of the text, cut to this number of chars,
* - tail (string, default: '&nbsp;&hellip;') - add this string to the input
* string if the string was cut.
*/
angular.module('ng').filter('cut', function () {
@EpokK
EpokK / ngIncludeLoaded.js
Last active December 20, 2015 18:49
How to run code after all ng-includes are loaded?
$scope.$on('$includeContentLoaded', function(event) {
console.log('another include was loaded', event.targetScope);
});
@EpokK
EpokK / markdown.js
Created August 12, 2013 06:15
markdown directive with Showdown
myApp.directive('markdown', function () {
var converter = new Showdown.converter();
return {
restrict: 'AE',
link: function (scope, element, attrs) {
if (attrs.markdown) {
scope.$watch(attrs.markdown, function (newVal) {
var html = converter.makeHtml(newVal);
element.html(html);
});

This is a fork of a fork of Bootstrap Typeahead that adds minimal but powerful extensions.

  • Support for delaying the lookup (good for preventing too many AJAX requests)
  • Some fixes regarding the data fed to the onselect callback

For the proper source, and other examples, please see the original gist and the extended version

Example showing off all the above features

@EpokK
EpokK / cookie.js
Created August 12, 2013 14:21
lire/écrire/delete et manger des cookies
@EpokK
EpokK / scrollDown.js
Last active December 27, 2015 16:19
scroll to bottom of div
window.onload = function () {
var objDiv = document.getElementById("idOfYourDiv");
objDiv.scrollTop = objDiv.scrollHeight;
}