ui-sref / ui-sref-active in modern Angular
ui-sref becomes routerLink and ui-sref-active becomes routerLinkActive. Links point to URL paths instead of state names.
Before · AngularJS
<nav>
<a ui-sref="users" ui-sref-active="active">Users</a>
<a ui-sref="userDetail({ id: user.id })">{{ user.name }}</a>
<a ui-sref="reports({ year: 2024 })" ui-sref-active-eq="active">Reports</a>
</nav>After · Angular@Component({
selector: 'app-nav',
imports: [RouterLink, RouterLinkActive],
template: `
<a routerLink="/users" routerLinkActive="active">Users</a>
<a [routerLink]="['/users', user().id]">{{ user().name }}</a>
<a routerLink="/reports" [queryParams]="{ year: 2024 }"
routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Reports</a>
`,
})
export class NavComponent {
user = input.required<User>();
}What changes
- State parameters become segments of the link array: ['/users', user.id].
- Query parameters go in a separate [queryParams] binding.
- ui-sref-active-eq maps to [routerLinkActiveOptions]="{ exact: true }".
Migration tip. Build a table of state names and their URL paths once, from the $stateProvider config. You will use the same table to convert every ui-sref and every $state.go call.
Related
Planning a migration or a micro-frontend platform?
In a 20-minute call you describe your situation and I tell you how I'd approach it. If it isn't worth doing yet, I'll say so.
Message me on LinkedIn