How to set the src attribute of a ngInclude element in the call back of a ajax request?

The example at ngInclude shows how to update the src attribute of a ngInclude element.

In a similar way, I am trying to assign the template path to a variable but in the callback of an ajax request. After the assignment, I am doing $rootScope.$apply($scope.setMainTemplatePath).
I am injecting the $rootScope also to the function containing the callback.

But I am getting the following error:

Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: function (){var a=d.url();if(!n||a!=f.absUrl())n++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),f.$$replace),f.$$replace=!1,h(a))});return n}; newVal: 30; oldVal: 29"],["fn: function (){var a=d.url();if(!n||a!=f.absUrl())n++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),f.$$replace),f.$$replace=!1,h(a))});return n}; newVal: 31; oldVal: 30"],["fn: function (){var a=d.url();if(!n||a!=f.absUrl())n++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),f.$$replace),f.$$replace=!1,h(a))});return n}; newVal: 32; oldVal: 31"],["fn: function (){var a=d.url();if(!n||a!=f.absUrl())n++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),f.$$replace),f.$$replace=!1,h(a))});return n}; newVal: 33; oldVal: 32"],["fn: function (){var a=d.url();if(!n||a!=f.absUrl())n++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),f.$$replace),f.$$replace=!1,h(a))});return n}; newVal: 34; oldVal: 33"]]
        at Error (unknown source)
        at Object.$digest (http://localhost:3000/assets/angular-1.0.1.min.js?body=1:85:413)
        at Object.$apply (http://localhost:3000/assets/angular-1.0.1.min.js?body=1:87:58)
        at HTMLDivElement.<anonymous> (http://localhost:3000/assets/web/angularjs/controllers.js?body=1:40:27)
        at HTMLDivElement.<anonymous> (http://localhost:3000/assets/jquery.js?body=1:3257:9)
        at HTMLDivElement.<anonymous> (http://localhost:3000/assets/jquery.js?body=1:2876:28)
        at Object.trigger (http://localhost:3000/assets/jquery.js?body=1:3145:12)
        at HTMLDivElement.<anonymous> (http://localhost:3000/assets/jquery.js?body=1:3782:17)
        at Function.each (http://localhost:3000/assets/jquery.js?body=1:659:20)
        at [object Object].each (http://localhost:3000/assets/jquery.js?body=1:272:17) 

Please point out if I have missed anything or doing anything wrong. Thanks.

You shouldn't be calling scope.$apply unless you are dealing with something outside of angular's control. More code would help.