\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{addyosami} \pdfinfo{ /Title (ng2.pdf) /Creator (Cheatography) /Author (addyosami) /Subject (ng2 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A32929} \definecolor{LightBackground}{HTML}{F9F1F1} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{ng2 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{addyosami} via \textcolor{DarkBackground}{\uline{cheatography.com/33837/cs/10570/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}addyosami \\ \uline{cheatography.com/addyosami} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 25th January, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.09494 cm} x{3.88206 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{@Component}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{template} & define template (using \textbackslash{}`) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{templateUrl} & define template \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} host & the element in which a component is attached to \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{helpers}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{(click)} & js default, propagates the `click` event to {\bf{all}} the parent components. So if want to not allow this, `return false` \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{syntax}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textless{}inventory-app\textgreater{}\textless{}/inventory-app\textgreater{} // or \newline \textless{}div inventory-app\textgreater{}\textless{}/div\textgreater{} \newline \newline input: {[}'name'{]} // or \newline @Input() name: string // or \newline input: {[}'inside: outside'{]} // avoid \newline @Input('outside') inside: string // avoid \newline \newline // set classes {\bf{conditionally}} \newline {[}class.selected{]}="isSelected(myProduct)" \newline \newline // (p122/644) \newline src="\{\{product.imageUrl\}\} // wrong \newline {[}src{]}="product.imageUrl" // right} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{best practices}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{use `template` when the view is not much \& vice versa. the drawback of using `template` is not having syntax hightlight} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{using the `host` option is nice because it means we can {\bf{encapsulate}} the `app-article` marup {\bf{within}} our component. By using the `host` option, we're able to configure our {\bf{host element}} from {\bf{within}} the component.} \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{isolate}} the data structures from the component code} \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{law of demeter}} a given object should assume as little as possible about the structure or properties of other objects} \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{link="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model"\}\}{\bf{fat models, skinny controllers}} \{\{/link\}\}} \tn % Row Count 16 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{when buiding a new angular app, mockup the design \& then break it down into components} \tn % Row Count 18 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{normally, author wouldn't pass more than 5 arguments to a function.} \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.23965 cm} x{2.73735 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{cli}} \tn % Row 0 \SetRowColor{LightBackground} watchman & OSX: `brew`, Linux: embercli, Window: native Nodejs watcher \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} ng new `app` & create a new ng2 app \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} ng serve & run app through `http` built in. `Window`: `-{}-host 0.0.0.0` \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} ng generate component `component` & create a new component \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{resources}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{link="https://angular.io/docs/ts/latest/guide/style-guide.html"\}\}Angular Style Guide\{\{/link\}\}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{link="https://en.wikipedia.org/wiki/Observer\_pattern"\}\}Observer Pattern\{\{/link\}\}} \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{terminology}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`\#newtitle`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}is called `resolve`. makes the variable `newtitle` available to the {\bf{expressions}} within the {\bf{view}}. `newtitle` is an {\bf{object}} (typeof `HTMLInputElement`) that represents this `input` DOM element} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`newtitle`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}template variable} \tn % Row Count 8 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`Article{[}{]}` or `Array\textless{}Article\textgreater{}`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`generics`} \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`\{\{ \}\}`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}template binding} \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`private currentProduct: Product`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}local component state} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{knowledge}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{- one of the big ideas behind Angular is the idea of `components`. \{\{nl\}\}- the fundamental idea behind `components`: we'll teach the browser {\bf{new tags}} that have custom functionality.\{\{nl\}\}- `components` are the {\bf{new}} version of `directives` `ng-1`} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{angular1's {\bf{dependency injection}} used the {\bf{annotation}} concept behind the scenes} \tn % Row Count 8 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{when boot an Angular app, you're not booting a component directly, but instead you create an `NgModule` which points to the component you want to load.} \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{you have to declare {\bf{components}} in a `NgModule` before you can use them in your templates} \tn % Row Count 14 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Angular 1, {\bf{directives}} match globally. Angular 2, need to {\bf{explicitly}} specify {\bf{which components}} you want to use} \tn % Row Count 17 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{JavaScript, {\bf{by default}}, {\bf{propagates}} the `click` event to {\bf{all the parent components}}} \tn % Row Count 19 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`href=""` (empty link) === {\bf{reload page}}} \tn % Row Count 20 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{an angular2 is nothing more than a {\bf{tree}} of {\bf{components}}. {\bf{top level Component}} is the application itself.that's what the browser will render when {\bf{booting}} (a.k.a {\bf{bootstrapping}}) the app.} \tn % Row Count 25 (+ 5) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`@Component` annotation is where you configure your component. Primary, `@Component` will configure how the {\bf{outside world}} will interact with your component.} \tn % Row Count 29 (+ 4) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`{[}{]}`: `input`, `()`: `output`. \{\{nl\}\}{\bf{Data flows in}} to your component via {\bf{input bindings}} and {\bf{events flow out}} of your component through {\bf{output binding}}.\{\{nl\}\}Think of the set of {\bf{input}} + {\bf{output bindings}} as defining the {\bf{public API}} of your component.} \tn % Row Count 35 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{knowledge (cont)}} \tn % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{In Angular, you send data out of components via {\bf{outputs}}.} \tn % Row Count 2 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`(onProductSelected)`: the name of the {\bf{output}} we want to {\bf{listen}} on\{\{nl\}\}`productWasSelected`: the function we want to call when {\bf{something new}} is on this {\bf{output}}\{\{nl\}\}`\$event`: special variable that represents the thing emitted on the {\bf{output}}} \tn % Row Count 8 (+ 6) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{when we specify that a {\bf{component}} takes an {\bf{input}}, it is expected that the definition class will have {\bf{an instance variable}} that will receive the value} \tn % Row Count 12 (+ 4) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`\textless{}button (click)="increase()"\textgreater{}Inc\textless{}/button\textgreater{}`\{\{nl\}\}In this case, the {\bf{event}} is {\bf{internal}} to the {\bf{component}}. we can also expose {\bf{public event}} (component `output`) that allow the component to talk to the {\bf{outside}} world} \tn % Row Count 17 (+ 5) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{An `EventEmitter` is simply {\bf{an object}} that helps you implement the `Observer Pattern`. That is, it's an object that can maintain a list of {\bf{subscribers}} and {\bf{publish}} events to them.} \tn % Row Count 21 (+ 4) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{When we assign an `EventEmitter` to an {\bf{output}} Angular {\bf{automatically subscribes}} for us. But can add subscriptions by {\bf{our own}}.} \tn % Row Count 24 (+ 3) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{every component must be declared in one `NgModule` before it can be used in a template} \tn % Row Count 26 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The {\bf{recommended way}} in Angular 2, and in many modern web frameworks (such as React), is to adopt a pattern of {\bf{one-way data binding}}. That is, your {\bf{data flows only down through components}}. If you need to {\bf{make changes}}, you {\bf{emit events}} that cause changes to happen {\bf{at the top}} which then trickle down.} \tn % Row Count 33 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}