Migrating a legacy AngularJS app to Angular (2+) is a multi-step process that involves strategic planning, gradual refactoring, and leveraging Angular’s Upgrade Module (@angular/upgrade
). The goal is to run both frameworks side by side temporarily and progressively move toward a full Angular architecture.
๐ Migration Strategy Overview (Step-by-Step)
✅ 1. Assess and Audit the AngularJS App
-
Inventory of all components, services, directives, filters
-
Identify areas of complexity (e.g., custom directives, stateful services)
-
Map routing and state management approaches (e.g.,
$stateProvider
,$scope
,$rootScope
)
✅ 2. Prepare the AngularJS App
-
Upgrade to AngularJS 1.7.x (last LTS version)
-
Use component-based architecture (
.component()
instead of.controller()
and.directive()
) -
Ensure strict DI annotations for compatibility with AOT
✅ 3. Set Up the Hybrid App (Angular + AngularJS)
Use Angular’s @angular/upgrade
module:
Create a hybrid bootstrap using UpgradeModule
:
✅ 4. Migrate Services and Components Gradually
๐ก Downgrade Angular services/components for use in AngularJS:
๐ก Upgrade AngularJS services/components for use in Angular:
✅ 5. Route Partitioning
-
Use AngularJS routing for legacy views, Angular Router for new views
-
Optionally use ui-router hybrid to manage routing across both
✅ 6. Move Feature-by-Feature
-
Start with isolated features (e.g., a form or dashboard widget)
-
Build new features in Angular only
-
Gradually replace AngularJS views, services, directives with Angular equivalents
✅ 7. Remove AngularJS
-
Once all features are migrated, remove
UpgradeModule
-
Clean up legacy code and dependencies
๐ง Best Practices
-
Keep Angular and AngularJS code in separate folders (
/legacy
,/app
) -
Use shared services for data access (migrated early)
-
Avoid deep coupling between AngularJS and Angular parts
-
Automate tests to ensure functional parity
๐งช Tools That Help
-
Linting tools to detect non-upgradeable patterns