I'm a noob in AngularJS, therefore sorry for stupid questions.
What I'd like to do is to use the same application in two (or more) places on a page. For that I've created two HTML elements and have bound my app to then using ng-app. The first ng-app would be bootstrapped automatically by angular, the second one has to be bootstrapped manually, which is what I did.
Everything works so far, however when it comes to routing and $location service I've hit a brick wall. Basically if the location is changed using $location service, accessing the route parameter from another instance of the ng-app throws an exception: "[$rootScope:infdig] 10 $digest() iterations reached. Aborting!"
The following is the code I'm trying to get to work:
<table>
<tr>
<td>
<div>
<div ng-app="mod1App" class="mod1">
<ng-view></ng-view>
</div>
</div>
</td>
<td>
<div>
<div ng-app="mod1App" class="mod2">
<ng-view></ng-view>
</div>
</div>
</td>
</tr>
</table>
<script>
var rootScopes = [];
var syncStarted = false;
var mod1App = angular.module("mod1App", ["ngRoute"]);
mod1App.config(function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: '/temp/main.html',
controller: 'MainCtrl'
})
.when("/edit/:param1", {
templateUrl: '/temp/edit.html',
controller: 'EditCtrl'
})
});
mod1App.controller("MainCtrl", function ($route, $scope, $location) {
$scope.testClick = function (e) {
if (e) e.preventDefault();
$scope.param = $route.current.params.param1;
}
$scope.testNavClick = function () {
syncStarted = false;
$location.path("/edit/" + Math.random());
}
});
mod1App.controller("EditCtrl", function ($route, $scope, $location) {
$scope.testClick = function (e) {
if (e) e.preventDefault();
$scope.param = $route.current.params.param1;
}
$scope.testNavClick = function () {
syncStarted = false;
$location.path("/");
}
});
mod1App.run(function ($rootScope) {
rootScopes.push($rootScope);
$rootScope.$on("$locationChangeSuccess", function (event, next, current) {
if (syncStarted == false) {
syncStarted = true;
for (var i = 0; i < rootScopes.length; i++) {
if (rootScopes[i] !== $rootScope) {
//synchronizing scopes
rootScopes[i].$apply();
}
}
}
});
});
angular.bootstrap($(".mod2")[0], ["mod1App", "ngRoute"]);
</script>
Thank you for your help!