Skip to content

Instantly share code, notes, and snippets.

View carmichaelize's full-sized avatar

Scott Carmichael carmichaelize

View GitHub Profile
@carmichaelize
carmichaelize / child-1.component.ts
Last active September 7, 2018 08:15
Angular2 component factory resolver example
import { Component } from '@angular/core';
import { ChildBaseComponent } from './child-base.component';
@Component({
template: `This is Child Component 1`
})
export class Child1Component extends ChildBaseComponent {
constructor(){
//Run base constructor
@carmichaelize
carmichaelize / angularjs-$compile.js
Last active July 18, 2018 09:30
AngularJS $compile example
function MyDirective ($templateRequest, $compile) {
var templateBase = '/directives/my_directive/views/';
var childTemplates = {
child1: 'child1.html',
child2: 'child2.html'
};
return {
@carmichaelize
carmichaelize / foo-component.jsx
Created November 3, 2017 08:55
React — Importing Global Variables from the Window Object
import React, { Component } from 'react';
import Foo from './foo-service';
class FooComponent extends Component {
foo = Foo;
render() {
return (<div>{this.foo.name}</div>)
}
}
@carmichaelize
carmichaelize / twitter_regex.php
Last active September 17, 2017 08:54
Twitter Links, Mentions and Hashtag Regex
<?php
//Urls
$tweet = preg_replace('/(http[^\s]+)/', '<a target="_blank" class="tweet-link" href="$1">$1</a>', $tweet->text);
//Mentions
$tweet = preg_replace('/\@([a-zA-Z1-9]+)/', '<a title="@$1" target="_blank" href="https://twitter.com/$1" class="tweet-user">@$1</a>', $string);
//Hashtags
$tweet = preg_replace('/#([a-zA-Z1-9]+)/', '<a title="#$1" target="_blank" href="https://twitter.com/search?q=%23$1&src=hash" class="tweet-hashtag">#$1</a>', $string);
@carmichaelize
carmichaelize / foo-bar.jsx
Last active July 19, 2017 20:07
Shallow Rendering a Redux Component in React Tests
import React, { Component } from 'react';
import { connect } from 'react-redux';
export class FooBar extends Component {
greet() {
return `Hello ${store.foo} ${store.bar}!`;
}
render() {
@carmichaelize
carmichaelize / is-online.component.ts
Created February 12, 2017 14:12
Testing for an internet connection.
import { Component } from '@angular/core';
@Component({
template: `
<h1>Is Online: {{ isOnline }}</h1>
<span (click)="clickHandler()">Click Me</span>
`,
})
export class MyComponent {
private baseUrl = 'http://www.test.com';
@carmichaelize
carmichaelize / capitalize.pipe.ts
Last active August 21, 2016 20:36
Angular2 string capitalisation pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'capitalize'
})
export class Capitalize implements PipeTransform {
transform(str: string, all: boolean) {
//Capitalize all the words
if (all) {
@carmichaelize
carmichaelize / angular-2-component-resolver.ts
Last active July 28, 2016 20:17
Angular2 component resolver example
import {Component, Input, ViewChild, ViewContainerRef, ComponentResolver, ComponentFactory} from '@angular/core';
import {Child1Component} from './child1.component';
import {Child2Component} from './child2.component';
@Component({
selector: 'parent',
template: `
<div #target></div>
`
})
// Here You can type your custom JavaScript...
jQuery.fn.highlight = function (str, className) {
var regex = new RegExp(str, "gi");
return this.each(function () {
$(this).contents().filter(function() {
return this.nodeType == 3 && regex.test(this.nodeValue);
}).replaceWith(function() {
return (this.nodeValue || "").replace(regex, function(match) {
return "<span class=\"" + className + "\">" + match + "</span>";
});
@carmichaelize
carmichaelize / transclusion.js
Created November 8, 2015 13:45
Custom Angular directive transclusion functions
//Shared scope
function myDirective() {
return {
restrict: 'EA',
templateUrl: 'my-directive.html',
transclude: true,
scope: false,
controllerAs: 'ctrl',
link: function(scope, element, attrs, ctrl, transclude) {
transclude(scope, function(clone, scope) {