Skip to content

Instantly share code, notes, and snippets.

View twerske's full-sized avatar

Emma Twersky twerske

View GitHub Profile
@twerske
twerske / angular.json
Last active May 13, 2022 20:33
Experimental ESM Application Builds
"builder": "@angular-devkit/build-angular:browser"
"builder": "@angular-devkit/build-angular:browser-esbuild"
@twerske
twerske / my.component.spec.ts
Created May 13, 2022 20:29
hasHarness and getHarnessOrNull in Component Test Harnesses
const loader = TestbedHarnessEnvironment
.loader(fixture);
const hasButton = await loader
.hasHarness(MatButtonHarness)
const buttonHarnessOrNull = await loader
.getHarnessOrNull(MatButtonHarness);
@twerske
twerske / menu.component.html
Created May 13, 2022 20:28
New primitives in the Angular CDK
<ul cdkTargetMenuAim cdkMenuBar>
<li cdkMenuItem
[cdkMenuTriggerFor]="file">
File
</li>
</ul>
<ng-template #file>
<ul cdkMenu cdkTargetMenuAim>
<li cdkMenuItem>Open</li>
</ul>
@twerske
twerske / my.component.ts
Created May 13, 2022 20:27
NgModel OnPush
@Component({
selector: 'my-component',
template: `
<child [ngModel]="value"></child>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
class MyComponent {}
@twerske
twerske / embedded-view-directive.ts
Last active May 13, 2022 20:26
Optional injectors in Embedded Views
viewContainer.createEmbeddedView(templateRef, context, {
injector: injector,
})
@twerske
twerske / my.component.ts
Created May 13, 2022 20:25
Bind to protected component members
@Component({
selector: 'my-component',
template: '{{ message }}', // Now compiles!
})
export class MyComponent {
protected message: string = 'Hello world';
}
@twerske
twerske / my-directive.ts
Created May 13, 2022 20:24
Tree-shakeable error messages
@Component({...})
class MyComponent {}
@Directive({...})
class MyDirective extends MyComponent {} // throws an error at runtime
// Before v14 the error is a string:
> Directives cannot inherit Components. Directive MyDirective is attempting to extend component MyComponent.
// Since v14 the error code makes this tree-shakeable:
@twerske
twerske / tsconfig.json
Last active June 6, 2022 18:10
Configure extended developer diagnostics
{
"angularCompilerOptions": {
"extendedDiagnostics": {
// The categories to use for specific diagnostics.
"checks": {
// Maps check name to its category.
"invalidBananaInBox": "error"
"nullishCoalescingNotNullable": "warning"
},
// The category to use for any diagnostics not listed in `checks` above.
@twerske
twerske / diagnostic.ts
Created May 13, 2022 17:37
Catch the invalid "Banana in a box" error on your two-way data bindings
Warning: src/app/app.component.ts:7:25 - warning NG8101: In the two-way binding syntax the parentheses should be inside the brackets, ex. '[(fruit)]="favoriteFruit"'.
Find more at https://angular.io/guide/two-way-binding
7 <app-favorite-fruit ([fruit])="favoriteFruit"></app-favorite-fruit>
~~~~~~~~~~~~~~~~~~~~~~~~~
@twerske
twerske / app-routing.module.ts
Created May 13, 2022 17:36
Streamlined page title accessibility with a TitleStrategy
const routes: Routes = [{
path: 'home',
component: HomeComponent
}, {
path: 'about',
component: AboutComponent,
title: 'About Me' // <-- Page title
}];
@Injectable()