@@ -417,6 +417,7 @@ angular.module('ui.scroll', [])
417417 }
418418
419419 function Adapter ( $attr , viewport , buffer , adjustBuffer , element ) {
420+ const hasViewport = ! ! viewport . scope ( ) ;
420421 const viewportScope = viewport . scope ( ) || $rootScope ;
421422 let disabled = false ;
422423 let self = this ;
@@ -514,31 +515,48 @@ angular.module('ui.scroll', [])
514515 let target = match [ 1 ] ;
515516 let onControllerName = match [ 2 ] ;
516517
517- let parseControllers = ( controllerName , as = false ) => {
518+ // ng-controller attr based DOM parsing
519+ let parseNgCtrlAttrs = ( controllerName , as = false ) => {
518520 let candidate = element ;
519521 while ( candidate . length ) {
522+ let candidateScope = candidate . scope ( ) ;
520523 let candidateName = ( candidate . attr ( 'ng-controller' ) || '' ) . match ( / ( \w (?: \w | \d ) * ) (?: \s + a s \s + ( \w (?: \w | \d ) * ) ) ? / ) ;
521524 if ( candidateName && candidateName [ as ? 2 : 1 ] === controllerName ) {
522- scope = candidate . scope ( ) ;
523- break ;
525+ scope = candidateScope ;
526+ return true ;
524527 }
525528 candidate = candidate . parent ( ) ;
526529 }
527530 } ;
528531
529- if ( onControllerName ) { // 'on' syntax parsing
532+ // scope based DOM pasrsing
533+ let parseScopes = ( controllerName ) => {
534+ let candidate = element ;
535+ while ( candidate . length ) {
536+ let candidateScope = candidate . scope ( ) ;
537+ if ( candidateScope && candidateScope . hasOwnProperty ( controllerName ) && candidateScope [ controllerName ] . constructor . name === 'controller' ) {
538+ scope = candidateScope ;
539+ return true ;
540+ }
541+ candidate = candidate . parent ( ) ;
542+ }
543+ } ;
544+
545+ if ( onControllerName ) { // 'on' syntax DOM parsing (adapter="adapter on ctrl")
530546 scope = null ;
531- parseControllers ( onControllerName ) ;
547+ parseNgCtrlAttrs ( onControllerName ) ;
532548 if ( ! scope ) {
533549 throw new Error ( 'Failed to locate target controller \'' + onControllerName + '\' to inject \'' + target + '\'' ) ;
534550 }
535551 }
536- else { // try to parse with 'Controller As' syntax
552+ else { // try to parse DOM with 'Controller As' syntax (adapter="ctrl.adapter")
537553 let controllerAsName ;
538554 let dotIndex = target . indexOf ( '.' ) ;
539555 if ( dotIndex > 0 ) {
540556 controllerAsName = target . substr ( 0 , dotIndex ) ;
541- parseControllers ( controllerAsName , true ) ;
557+ if ( ! parseNgCtrlAttrs ( controllerAsName , true ) && ! hasViewport ) {
558+ parseScopes ( controllerAsName ) ; // the case of custom Directive/Component
559+ }
542560 }
543561 }
544562
0 commit comments