我正在尝试通过angularjs模块实现twitter bootstrap的scrollspy。我有一些来自How to implement a scrollspy in angular.js the right way?
的代码但我得到“TypeError:Object [object Object]没有方法'scrollspy'”来自以下代码:
var scrollSpyModule = angular.module('scrollSpyModule', []);
scrollSpyModule.directive('scrollSpy', function($timeout){
return {
restrict: 'A',
link: function(scope, elem, attr) {
console.log($(elem));
var offset = parseInt(attr.scrollOffset, 10)
if(!offset) offset = 10;
console.log("offset: " + offset);
$(elem).scrollspy({ "offset" : offset});
scope.$watch(attr.scrollspy, function(value) {
$timeout(function() {
elem.scrollspy('refresh', { "offset" : offset})
}, 1);
}, true);
}
}
});
令人困惑,因为console.log($(elem))在其原型中显示了一个带有scrollspy的元素。
我想我已经包含了所有依赖项:
<script src="@routes.Assets.at("javascripts/jquery.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/bootstrap.js")" type="text/javascript"></script>
当我从主模板中调用它时没有问题:
<script>
$("#bs-example-navbar-collapse-1").scrollspy({ "offset" : 12});
console.log("No problem...");
</script>