Angular
A .ng.mx file is an Angular component module with MX as the value of template:. The class, the decorator, inputs, signals and injection are the TypeScript you already write, and the Angular compiler builds the result. The template changes.
This is one component, as Angular and then as .ng.mx. Card is an MX tag with a title and a footer projection.
import { NgClass } from "@angular/common";
import { Component, Input } from "@angular/core";
import { Card } from "./tags/card";
import type { Member } from "./members";
@Component({
selector: "app-team",
imports: [Card, NgClass],
template: `
<mx-card>
<ng-container ngProjectAs="[title]">
Team <span class="count">{{ shown.length }}</span>
</ng-container>
<input name="q" class="search" type="search" [value]="query" (input)="search($event)" />
@if (shown.length) {
<ul class="members">
@for (member of shown; track member.id) {
<li [ngClass]="{ admin: member.admin }">
{{ member.name }}
@for (team of member.teams; track team) {
<span class="tag">{{ team }}</span>
}
</li>
}
</ul>
} @else {
<p class="empty">Nobody matches “{{ query }}”.</p>
}
<ng-container ngProjectAs="[footer]">
<button class="button" (click)="clear()">Clear</button>
</ng-container>
</mx-card>
`,
})
export class Team {
@Input() members: Member[] = [];
protected query = "";
protected get shown() {
return this.members.filter((member) => member.name.includes(this.query));
}
protected search(event: Event) {
this.query = (event.target as HTMLInputElement).value;
}
protected clear() {
this.query = "";
}
}
import { Component, Input } from "@angular/core";
import Card from "./tags/card.mx";
import type { Member } from "./members";
@Component({
selector: "app-team",
template: <Card>
<@title>Team <span.count>${shown.length}</span></@title>
<input:q.search type="search" value=query onInput=search/>
<if=shown.length>
<ul.members>
<for|member| of=shown by=(member => member.id)>
<li class={ admin: member.admin }>
${member.name}
<for|team| of=member.teams by=(team => team)><span.tag>${team}</span></for>
</li>
</for>
</ul>
</if>
<else>
<p.empty>Nobody matches “${query}”.</p>
</else>
<@footer><button.button onClick=clear>Clear</button></@footer>
</Card>,
})
export class Team {
@Input() members: Member[] = [];
protected query = "";
protected get shown() {
return this.members.filter((member) => member.name.includes(this.query));
}
protected search(event: Event) {
this.query = (event.target as HTMLInputElement).value;
}
protected clear() {
this.query = "";
}
}
What changed:
- No
imports:array. MX addsCardandNgClassto the decorator and writes theirimportlines, because it knows what the template uses. <@title>and<@footer>are attribute tags: content projected intoCard’s named slots, withoutngProjectAs.<if>/<else>and<for>compile to@if,@elseand@for … track.<input:q.search>,<span.tag>arenameandclass.class={ admin: member.admin }is[ngClass].value=query,onInput=searchare plain attributes; MX picks[value]and(input).- The template is not a string. It is highlighted, formatted and checked like code.
Setup
bun add -d @mxlang/angular
// package.json
{
"mx": { "host": "angular" },
"scripts": {
"start": "mx-angular build && concurrently -k \"mx-angular watch\" \"ng serve\"",
"build": "mx-angular build && ng build"
}
}
mx-angular writes team.component.ts beside team.component.ng.mx, and Angular compiles that file like any other. Add the emitted files to .gitignore. .ng.mx files are found anywhere in the project, with no configuration.
For type errors in the editor, add { "name": "@mxlang/typescript-plugin" } to compilerOptions.plugins; in CI, run mx-tsc --noEmit. Both also run Angular’s own template checker on every template: and report at the line you wrote: see Diagnostics.
What stays Angular, what MX adds
Stays Angular: the class, @Component and its other fields, inputs and outputs, signals, dependency injection, the router, ng build and ng serve. A hand-written component is called by its selector, as in any template: <app-product-list/>.
MX adds: the template syntax above, compiled to Angular’s block syntax (@if, @for, @let), and the imports: bookkeeping. There is no MX runtime in the browser.
Two rules to know first
- Handlers are references or expression arrows. Write
onClick=clearoronClick=(event => save(event)). Angular templates have no statement bodies, so the method formonClick() { … }and a block-bodied arrow are compile errors here. - Do not import a name that is also an element.
import { input } from "@angular/core"makes<input>a call of that function; use@Input()or alias the import.
Go deeper
.ng.mxin detail: where the template goes, fragments for several roots,imports:rules, build output.- What MX compiles to: the table, how an attribute becomes a property or attribute binding, events.
- Diagnostics: TypeScript and Angular template errors in
mx-tscand the editor. - Page templates, MX tags and the CLI: a
.mxbeside a hand-written component, tags such asCard, andmx-angular build,watchandmap.