简体   繁体   中英

Angular run jQuery code for all site

I want to implement http://areaaperta.com/nicescroll/ in my project (AngularJS based project).

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? 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.

HTML:

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

</body>
…

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();
            }
        }
    }
});
…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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