2013-12-20 7 views
1

AngularJS를 처음 사용했습니다. 저는 AngularJS 1.2.5와 Bootstrap 3.0을 사용하고 있습니다. 내 응용 프로그램에 ScrollSpy를 포함하려고합니다. 그러나 몇 가지 문제가 있습니다. 코드를 통합하려고하는데 here입니다. 현재, 내 코드는 다음과 같습니다AngularJS에서 ScrollSpy 사용

index.html을

<div class="row" scroll-spy> 
    <div class="col-md-3 sidebar"> 
    <ul style="position:fixed;" class="nav nav-pills nav-stacked"> 
     <li class="active" spy="overview"><a href="#overview">Overview</a></li> 
     <li spy="main"><a href="#main">Main Content</a></li> 
     <li spy="summary"><a href="#summary">Summary</a></li> 
     <li spy="links"><a href="#links">Other Links</a></li> 
    </ul> 
    </div> 

    <div class="col-md-9 content"> 
    <h3 id="overview">Overview</h3> 
    Lorem Ipsum Text goes here... 

    <h3 id="main">Main Body</h3> 
    Lorem Ipsum Text goes here... 

    <h3 id="summary">Summary</h3> 
    Lorem Ipsum text goes here... 

    <h3 id="links">Other Links</h3> 
    </div> 
</div> 

index.html.js

angular.module('td.controls.scrollSpy', []) 
    .directive('spy', function ($location) { 
    return { 
     restrict: 'A', 
     require: '^scrollSpy', 
     link: function (scope, elem, attrs, scrollSpy) { 
     var _ref; 
     if ((_ref = attrs.spyClass) == null) { 
      attrs.spyClass = 'current'; 
     } 
     elem.click(function() { 
      return scope.$apply(function() { 
      return $location.hash(attrs.spy); 
      }); 
     }); 
     return scrollSpy.addSpy({ 
      id: attrs.spy, 
      'in': function() { 
      return elem.addClass(attrs.spyClass); 
      }, 
      out: function() { 
      return elem.removeClass(attrs.spyClass); 
      } 
     }); 
     } 
    }; 
    }) 
    .directive('scrollSpy', function ($location) { 
    return { 
     restrict: 'A', 
     controller: function ($scope) { 
     $scope.spies = []; 
     return this.addSpy = function (spyObj) { 
      return $scope.spies.push(spyObj); 
     }; 
     }, 
     link: function (scope, elem, attrs) { 
     var spyElems; 
     spyElems = []; 
     scope.$watch('spies', function (spies) { 
      var spy, _i, _len, _results; 
      _results = []; 
      for (_i = 0, _len = spies.length; _i < _len; _i++) { 
      spy = spies[_i]; 
      if (spyElems[spy.id] == null) { 
       _results.push(spyElems[spy.id] = elem.find('#' + spy.id)); 
      } else { 
       _results.push(void 0); 
      } 
      } 
      return _results; 
     }); 
     return $($window).scroll(function() { 
      var highlightSpy, pos, spy, _i, _len, _ref; 
      highlightSpy = null; 
      _ref = scope.spies; 
      for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
      spy = _ref[_i]; 
      spy.out(); 
      spyElems[spy.id] = spyElems[spy.id].length === 0 ? elem.find('#' + spy.id) : spyElems[spy.id]; 

      if (spyElems[spy.id].length !== 0) { 
       if ((pos = spyElems[spy.id].offset().top) - $window.scrollY <= 0) { 
       spy.pos = pos; 
       if (highlightSpy == null) { 
        highlightSpy = spy; 
       } 
       if (highlightSpy.pos < spy.pos) { 
        highlightSpy = spy; 
       } 
       } 
      } 
      } 
      return highlightSpy != null ? highlightSpy['in']() : void 0; 
     }); 
     } 
    }; 
    }) 
; 

내가 몇 가지 오류를 얻을 브라우저에서 실행합니다. 내가 처음 실행할 때, 나는 내 브라우저 콘솔에서 다음과 같은 오류를 참조하십시오

TypeError: Object function (spyObj) { return $scope.spies.push(spyObj); } has no method 'addSpy' 
ReferenceError: $window is not defined 

내가 알아낼 수없는) 나는 이러한 오류를 받고 있어요 왜 또는 b)이 기본 예제가 작동하도록하는 방법에 대해 설명합니다. AngularJS에서 scrollspy를 사용하는이 접근법을 정말 좋아합니다. 그것은 내가 본 가장 깨끗한 구현입니다. 그런 이유로, 나는이 일을하는 방법을 알아 내기를 원합니다.

답변

2

저는 최근 알렉산더의 솔루션을 가로 질러 번역 과정을 밟았습니다.

직접 질문에 대답하려면 $windowscrollSpy으로 가져와야합니다.

app.directive('scrollSpy', function ($window) { 
    return { 
    restrict: 'A', 
    controller: function ($scope) { 
     $scope.spies = []; 
     this.addSpy = function (spyObj) { 
     $scope.spies.push(spyObj); 
     }; 
    }, 
    link: function (scope, elem, attrs) { 
     var spyElems; 
     spyElems = []; 

     scope.$watch('spies', function (spies) { 
     var spy, _i, _len, _results; 
     _results = []; 

     for (_i = 0, _len = spies.length; _i < _len; _i++) { 
      spy = spies[_i]; 

      if (spyElems[spy.id] == null) { 
      _results.push(spyElems[spy.id] = elem.find('#' + spy.id)); 
      } 
     } 
     return _results; 
     }); 

     $($window).scroll(function() { 
     var highlightSpy, pos, spy, _i, _len, _ref; 
     highlightSpy = null; 
     _ref = scope.spies; 

     // cycle through `spy` elements to find which to highlight 
     for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
      spy = _ref[_i]; 
      spy.out(); 

      // catch case where a `spy` does not have an associated `id` anchor 
      if (spyElems[spy.id].offset() === undefined) { 
      continue; 
      } 

      if ((pos = spyElems[spy.id].offset().top) - $window.scrollY <= 0) { 
      // the window has been scrolled past the top of a spy element 
      spy.pos = pos; 

      if (highlightSpy == null) { 
       highlightSpy = spy; 
      } 
      if (highlightSpy.pos < spy.pos) { 
       highlightSpy = spy; 
      } 
      } 
     } 

     // select the last `spy` if the scrollbar is at the bottom of the page 
     if ($(window).scrollTop() + $(window).height() >= $(document).height()) { 
      spy.pos = pos; 
      highlightSpy = spy; 
     }   

     return highlightSpy != null ? highlightSpy["in"]() : void 0; 
     }); 
    } 
    }; 
}); 


app.directive('spy', function ($location, $anchorScroll) { 
    return { 
    restrict: "A", 
    require: "^scrollSpy", 
    link: function(scope, elem, attrs, affix) { 
     elem.click(function() { 
     $location.hash(attrs.spy); 
     $anchorScroll(); 
     }); 

     affix.addSpy({ 
     id: attrs.spy, 
     in: function() { 
      elem.addClass('active'); 
     }, 
     out: function() { 
      elem.removeClass('active'); 
     } 
     }); 
    } 
    }; 
}); 

위의 코드는 브라우저를 아래로 스크롤하면 메뉴의 마지막 spy 요소를 강조 지원, 원래 : 아래

.directive('scrollSpy', function ($location, $window) { 

내가 알렉산더의 코드의 한 완전한 번역입니다 코드는 그렇지 않았다. 당신이 작업하는 것이 습관 경우

0

if (spyElems[spy.id].offset() === undefined) { 
    // try to refind it 
    spyElems[spy.id] = elem.find('#' + spy.id); 
    if(spyElems[spy.id].offset() === undefined) 
     continue; 
} 
에 추종 조건

 if (spyElems[spy.id].offset() === undefined) { 
     continue; 
     } 

을 변경 NG-포함

관련 문제