AngularJS is a powerful framework, but developers often encounter runtime errors due to incorrect syntax, scope issues, dependency injection problems, or other misconfigurations. Below are some of the most common AngularJS runtime errors and how to fix them effectively.
1. $injector:unpr – Unknown Provider
Error Message:
Error: [$injector:unpr] Unknown provider: myServiceProvider <- myService
Cause:
- This happens when a service, factory, or provider is not registered properly or is missing in the module.
- It could also be due to incorrect dependency injection.
Fix:
- Ensure that the service is correctly registered:
app.service('myService', function() { this.getData = function() { return "Hello from Service!"; }; });
- Inject the dependency properly:
app.controller('myController', ['myService', function(myService) { console.log(myService.getData()); }]);
- For minified code, use the array notation to prevent dependency injection issues:
myApp.controller('myController', ['$scope', 'myService', function($scope, myService) { $scope.data = myService.getData(); }]);
2. $digest already in progress
Error Message:
Error: [$rootScope:inprog] $digest already in progress
Cause:
- This occurs when
$digest()
is triggered inside another digest cycle. - AngularJS automatically runs a digest cycle when events, HTTP calls, or directives update the UI, so manually calling
$apply()
inside it can cause this error.
Fix:
- Use
$applyAsync()
instead of$apply()
:$scope.$applyAsync(function() { $scope.message = "Updated Message"; });
- Check if
$digest()
is already in progress before calling$apply()
:if (!$scope.$$phase) { $scope.$apply(); }
3. $compile:tplrt – Template Must Have Exactly One Root Element
Error Message:
Error: [$compile:tplrt] Template must have exactly one root element
Cause:
- This error occurs when a directive’s template contains multiple root elements.
Fix:
Ensure the directive template has a single root element:
Incorrect:
template: '<h1>Title</h1><p>Paragraph</p>'
Correct:
template: '<div><h1>Title</h1><p>Paragraph</p></div>'
4. $rootScope:infdig – Infinite $digest Loop
Error Message:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Cause:
- This occurs when a watcher keeps triggering itself, leading to an infinite loop.
- It usually happens when
$watch()
modifies the watched variable inside its callback.
Fix:
- Use
$timeout
instead of modifying values inside$watch
:$scope.$watch('count', function(newValue, oldValue) { if (newValue !== oldValue) { $timeout(function() { $scope.count++; }); } });
- Ensure that watched expressions do not modify themselves:
$scope.$watch('data', function(newValue, oldValue) { if (newValue === oldValue) return; // Prevent unnecessary updates $scope.data = processData(newValue); });
5. $interpolate:interr – Interpolation Error
Error Message:
Error: [$interpolate:interr] Can't interpolate: {{ value }}
Cause:
- This occurs when AngularJS fails to evaluate an expression inside
{{ }}
due to syntax errors or undefined variables.
Fix:
- Ensure that the variable exists in the scope:
$scope.value = "Hello, AngularJS!";
- Check for syntax errors in expressions:
<p>{{ value }}</p> <!-- Ensure there are no typos -->
6. ngRepeat:dupes – Duplicate Key in ng-repeat
Error Message:
Error: [$compile:dupes] Duplicates in a repeater are not allowed
Cause:
- This happens when AngularJS detects duplicate objects in an
ng-repeat
loop.
Fix:
- Use
track by
with a unique identifier:<li ng-repeat="item in items track by item.id">{{ item.name }}</li>
- Ensure array items have unique values:
$scope.items = [ { id: 1, name: "Item 1" }, { id: 2, name: "Item 2" } ];
7. $http:badreq – Bad Request in HTTP Calls
Error Message:
Error: [$http:badreq] Bad Request
Cause:
- The
$http
request is missing required parameters or is improperly formatted.
Fix:
- Ensure the request has the correct format:
$http.post('/api/data', { name: "AngularJS" }) .then(function(response) { console.log(response.data); }) .catch(function(error) { console.error("Request failed:", error); });
- Check for missing headers:
$http({ method: 'POST', url: '/api/data', data: { name: "AngularJS" }, headers: { 'Content-Type': 'application/json' } });
8. $parse:syntax – Syntax Error in Expressions
Error Message:
Error: [$parse:syntax] Syntax Error: Token 'unexpected' is unexpected
Cause:
- This occurs due to incorrect syntax in an AngularJS expression.
Fix:
- Check for typos and missing operators:
<p>{{ 5 + 5 }}</p> <!-- Correct --> <p>{{ 5 + }}</p> <!-- Incorrect -->
- Ensure function calls in expressions are valid:
$scope.getMessage = function() { return "Hello, AngularJS!"; };
htmlCopyEdit<p>{{ getMessage() }}</p> <!-- Correct -->