diff options
| author | Tobias Bosch | 2013-11-21 21:54:59 -0800 | 
|---|---|---|
| committer | Tobias Bosch | 2013-11-21 22:20:11 -0800 | 
| commit | e6521e7491242504250b57dd0ee66af49e653c33 (patch) | |
| tree | 1a605bfc1a44c423ba7fec13ae94b984992bd0ab /src/ngRoute/directive/ngView.js | |
| parent | 0a7cbb33b06778833a4d99b1868cc07690a827a7 (diff) | |
| download | angular.js-e6521e7491242504250b57dd0ee66af49e653c33.tar.bz2 | |
fix(ngView): Don't throw when the ngView element contains content with directives.
Fixes #5069
Diffstat (limited to 'src/ngRoute/directive/ngView.js')
| -rw-r--r-- | src/ngRoute/directive/ngView.js | 64 | 
1 files changed, 35 insertions, 29 deletions
| diff --git a/src/ngRoute/directive/ngView.js b/src/ngRoute/directive/ngView.js index 3271ac0c..280bf423 100644 --- a/src/ngRoute/directive/ngView.js +++ b/src/ngRoute/directive/ngView.js @@ -199,37 +199,43 @@ function ngViewFactory(   $route,   $anchorScroll,   $compile,   $controller,            if (template) {              var newScope = scope.$new(); -            $transclude(newScope, function(clone) { -              clone.html(template); -              $animate.enter(clone, null, currentElement || $element, function onNgViewEnter () { -                if (angular.isDefined(autoScrollExp) -                  && (!autoScrollExp || scope.$eval(autoScrollExp))) { -                  $anchorScroll(); -                } -              }); - -              cleanupLastView(); - -              var link = $compile(clone.contents()), -                  current = $route.current; - -              currentScope = current.scope = newScope; -              currentElement = clone; - -              if (current.controller) { -                locals.$scope = currentScope; -                var controller = $controller(current.controller, locals); -                if (current.controllerAs) { -                  currentScope[current.controllerAs] = controller; -                } -                clone.data('$ngControllerController', controller); -                clone.children().data('$ngControllerController', controller); -              } -              link(currentScope); -              currentScope.$emit('$viewContentLoaded'); -              currentScope.$eval(onloadExp); +            // Note: This will also link all children of ng-view that were contained in the original +            // html. If that content contains controllers, ... they could pollute/change the scope. +            // However, using ng-view on an element with additional content does not make sense... +            // Note: We can't remove them in the cloneAttchFn of $transclude as that +            // function is called before linking the content, which would apply child +            // directives to non existing elements. +            var clone = $transclude(newScope, angular.noop); +            clone.html(template); +            $animate.enter(clone, null, currentElement || $element, function onNgViewEnter () { +              if (angular.isDefined(autoScrollExp) +                && (!autoScrollExp || scope.$eval(autoScrollExp))) { +                $anchorScroll(); +              }              }); + +            cleanupLastView(); + +            var link = $compile(clone.contents()), +                current = $route.current; + +            currentScope = current.scope = newScope; +            currentElement = clone; + +            if (current.controller) { +              locals.$scope = currentScope; +              var controller = $controller(current.controller, locals); +              if (current.controllerAs) { +                currentScope[current.controllerAs] = controller; +              } +              clone.data('$ngControllerController', controller); +              clone.children().data('$ngControllerController', controller); +            } + +            link(currentScope); +            currentScope.$emit('$viewContentLoaded'); +            currentScope.$eval(onloadExp);            } else {              cleanupLastView();            } | 
