Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing ScrollSpy in AngularJS
    primarykey
    data
    text
    <p>I'm new to AngularJS. I'm using AngularJS 1.2.5 and Bootstrap 3.0. I'm trying to include ScrollSpy in my app. However, I'm having some challenges. I'm trying to incorporate the code found <a href="https://gist.github.com/alxhill/6886760" rel="nofollow">here</a>. Currently, my code looks like this:</p> <p><strong>index.html</strong></p> <pre><code>&lt;div class="row" scroll-spy&gt; &lt;div class="col-md-3 sidebar"&gt; &lt;ul style="position:fixed;" class="nav nav-pills nav-stacked"&gt; &lt;li class="active" spy="overview"&gt;&lt;a href="#overview"&gt;Overview&lt;/a&gt;&lt;/li&gt; &lt;li spy="main"&gt;&lt;a href="#main"&gt;Main Content&lt;/a&gt;&lt;/li&gt; &lt;li spy="summary"&gt;&lt;a href="#summary"&gt;Summary&lt;/a&gt;&lt;/li&gt; &lt;li spy="links"&gt;&lt;a href="#links"&gt;Other Links&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div class="col-md-9 content"&gt; &lt;h3 id="overview"&gt;Overview&lt;/h3&gt; Lorem Ipsum Text goes here... &lt;h3 id="main"&gt;Main Body&lt;/h3&gt; Lorem Ipsum Text goes here... &lt;h3 id="summary"&gt;Summary&lt;/h3&gt; Lorem Ipsum text goes here... &lt;h3 id="links"&gt;Other Links&lt;/h3&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>index.html.js</strong></p> <pre><code>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 &lt; _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 &lt; _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 &lt;= 0) { spy.pos = pos; if (highlightSpy == null) { highlightSpy = spy; } if (highlightSpy.pos &lt; spy.pos) { highlightSpy = spy; } } } } return highlightSpy != null ? highlightSpy['in']() : void 0; }); } }; }) ; </code></pre> <p>When I run this in the browser I get several errors. When I initially run it, I see the following errors in my browser console:</p> <pre><code>TypeError: Object function (spyObj) { return $scope.spies.push(spyObj); } has no method 'addSpy' ReferenceError: $window is not defined </code></pre> <p>I can't figure out a) Why I'm getting these errors or b) how to get this basic example to work. I really like this approach to using scrollspy with AngularJS. It's the cleanest implementation I've seen. For that reason, I'd love to figure out how to get this working.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload