Skip to content

Instantly share code, notes, and snippets.

// Component file
// Where the service is injected
import { SelectionModel } from "@angular/cdk/collections";
@Component(...metaData)
export class AppComponent implements OnInit {
constructor(peopleSelection: PeopleSelectionService) {}
ngOnInit(): void {
console.log(
// Module file
// Injecting the service
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
// See here
@Injectable()
export class MyInjectableService {}
// Define the Module
// Inject the service
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
@NgModule({
imports: [BrowserModule],
import { SelectionModel } from "@angular/cdk/collections";
@Component(...metaData)
export class SelectionExample {
peopleSelection = new SelectionModel<Person>(true /* multiple */);
select(person: Person): void {
this.peopleSelection.select(person);
return;
}
@goughjo02
goughjo02 / typescript-example-1.ts
Last active February 25, 2019 12:41
With Typescript one can define Types, define what 'type' is returned from a method, of or define which 'type' an argument is.
// Here is a Person type
type Person {
name: string;
age: number;
}
// This function accepts a Person object and returns their name (a string)
function getName(person: Person): string {
return person.name;
}
// This function takes an arguement array of people and returns their combined age (a number)