Small Angular patterns that make a big codebase easier to live in

You cannot fix a 200k-line Angular app in a weekend. But a handful of small patterns, applied consistently, make the next year of maintenance dramatically less painful.

AI-generated editorial scene of the Angular symbol in a sunlit crimson and ivory architectural stairwell

Angular is opinionated by default, but at scale the defaults are not enough. These are the patterns we apply on every client Angular project that has gone past 50 components.

1. One feature = one folder = one route

src/app/
  features/
    posts/
      pages/         # routed components
      components/    # feature-local components
      services/
      stores/
      models/
      posts.routes.ts
  shared/            # cross-feature only
  core/              # singleton services

Each features/* folder owns its routes file and imports lazily from app.routes.ts:

export const routes: Routes = [
    {
        path: 'posts',
        loadChildren: () => import('./features/posts/posts.routes').then((m) => m.POSTS_ROUTES)
    }
]

Every feature can be deleted with rm -rf and the rest of the app keeps compiling. That is the test.

2. Inject the service, never reach into the state

Bad:

@Component({ /* ... */ })
export class PostListComponent {
    postStore = inject(PostStore)

    ngOnInit() {
        // Don't do this
        this.postStore.state.posts = [...this.postStore.state.posts, newPost]
    }
}

Good:

@Component({ /* ... */ })
export class PostListComponent {
    private posts = inject(PostService)

    ngOnInit() {
        this.posts.create(newPost)
    }
}

Components do not know the shape of state. They only know methods.

3. Signals for local state, stores for shared state

// Local, component-scoped
@Component({ /* ... */ })
export class FilterBarComponent {
    query = signal('')
    showResults = computed(() => this.query().length > 2)
}

// Shared, feature-scoped (using NgRx Signal Store or similar)
export const PostStore = signalStore(
    { providedIn: 'root' },
    withState<PostState>({ posts: [], loading: false }),
    withMethods((store, api = inject(PostApi)) => ({
        async load() {
            patchState(store, { loading: true })
            const posts = await lastValueFrom(api.list())
            patchState(store, { posts, loading: false })
        }
    }))
)

See NgRx SignalStore for the second pattern.

4. inject() in factory functions, not only classes

For testable route guards and resolvers:

export const authGuard: CanActivateFn = () => {
    const auth = inject(AuthService)
    const router = inject(Router)

    if (auth.isAuthenticated()) return true
    router.navigate(['/login'])
    return false
}

Classes for this are a ceremony you do not need.

5. Typed form builders

import { inject } from '@angular/core'
import { FormBuilder, Validators } from '@angular/forms'

const fb = inject(FormBuilder).nonNullable

const form = fb.group({
    title: ['', [Validators.required, Validators.maxLength(120)]],
    category: fb.control<'AI' | 'Tooling' | 'Gizmos' | 'Vankode'>('Vankode', Validators.required),
    publishedAt: fb.control<Date | null>(null)
})

form.value.title      // string
form.value.category   // 'AI' | 'Tooling' | 'Gizmos' | 'Vankode'

See typed forms docs. No more form.get('title')?.value as string patches.

6. NgOptimizedImage everywhere

<img
    ngSrc="/assets/hero.jpg"
    width="1600"
    height="900"
    priority
    alt="Editorial hero photograph"
/>

Built-in lazy loading, preconnect for CDNs, srcset generation. Docs.

7. Testing: mount components in TestBed with signals mocked

import { ComponentFixture, TestBed } from '@angular/core/testing'
import { signal } from '@angular/core'
import { PostList } from './post-list.component'
import { PostStore } from '../stores/post.store'

describe('PostList', () => {
    let fixture: ComponentFixture<PostList>

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [PostList],
            providers: [
                {
                    provide: PostStore,
                    useValue: {
                        posts: signal([{ id: '1', title: 'Hello' }]),
                        loading: signal(false)
                    }
                }
            ]
        })
        fixture = TestBed.createComponent(PostList)
        fixture.detectChanges()
    })

    it('renders posts from the store', () => {
        const text = fixture.nativeElement.textContent
        expect(text).toContain('Hello')
    })
})

Mock the store. Don't mock the template. Don't mock the change detector.

8. Lint for common mistakes

Add @angular-eslint and turn on:

  • @angular-eslint/prefer-standalone
  • @angular-eslint/use-lifecycle-interface
  • @angular-eslint/no-host-metadata-property
  • @typescript-eslint/no-floating-promises

These four rules catch roughly half of the pull-request comments a senior Angular dev would leave manually.

Angular at scale is about discipline, not tricks. These eight patterns are the discipline.