The way this works for ngRepeat is different. It works because it compiles each repetition of what you need to do here.
For example:
mainCtrl = function($scope, $compile, $element) { $scope.someScopeVar = ctrl1; $scope.changeCtrl = function(id) { $scope.someScopeVar = id === 1 ? ctrl1 : ctrl2; var elm = $element.find('div'); elm.replaceWith($compile(template)($scope)); } } var ctrl1 = function($scope) { $scope.name = 'World'; } var ctrl2 = function($scope) { $scope.name = 'John'; }
We assign each controller function $scope.someScopeVar , then we must compile the new element with the new controller and replace the old one.
I do not believe that angular has the ability to update the controller on the fly. May be a good idea for a new feature.
Fiddle
Another option is to use the mixin template to update the main area. You will not get a new shiny area, but it may work for your purposes.
mainCtrl = function($scope, $compile, $element) { angular.extend($scope, ctrl1()); $scope.changeCtrl = function(id) { angular.extend($scope, id === 1 ? ctrl1() : ctrl2()); } } var ctrl1 = function() { var obj = {}; obj.name = 'World'; return obj; } var ctrl2 = function() { var obj = {}; obj.name = 'John'; return obj; }
Fiddle
Mosho
source share