简体   繁体   English

所有站点的Angular运行jQuery代码

[英]Angular run jQuery code for all site

I want to implement http://areaaperta.com/nicescroll/ in my project (AngularJS based project). 我想在我的项目(基于AngularJS的项目)中实现http://areaaperta.com/nicescroll/

What I need to do is: 我需要做的是:

$(document).ready(

  function() { 

    $("html").niceScroll();

  }

);

However, where do I place this if I need it to run throughout the site and as soon as the DOM finishes loading? 但是,如果我需要它在整个站点中运行并且DOM完成加载后立即放置在哪里? Not sure if a directive is what I need, and if so, how would it be and where should I include/run it? 不知道指令是否是我所需要的,如果需要,它将如何?应该在哪里包含/运行它?

The best way to use jquery plugins in angular projects are the use of directives. 在有角项目中使用jquery插件的最佳方法是使用指令。

HTML: HTML:

…
<body ng-app="myApp" nicescroll>

</body>
…

JS: JS:

…
var app = angular.module('myApp', []);

app.directive('nicescroll', function() {
    return {
        restrict: 'A',
        link: function() {
            // check, if the jquery plugin is loaded and available
            if ($.fn.niceScroll) {
                $('html').niceScroll();
            }
        }
    }
});
…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM