【问题标题】:Initiate jQuery function by using AngularJS custom directive使用 AngularJS 自定义指令启动 jQuery 函数
【发布时间】:2019-09-17 22:37:55
【问题描述】:

这是我想调用$('.ams-items').slimScroll()函数的代码

angular.element(document).ready(function () {
  $('.ams-items').slimScroll({
    alwaysVisible: true
  });
}); 

请帮帮我!

【问题讨论】:

标签: jquery angularjs angularjs-directive


【解决方案1】:

如何在自定义指令中封装 jQuery 代码1

任何操作 DOM 的 jQuery 代码都应该封装在 custom directive 中,以便在 AngularJS 框架实例化元素时执行。

app.directive("mySlimScroll", function()
     return {
         link: postLink,
     };
     function postLink(scope,elem,attrs) {
         elem.slimScroll({
             alwaysVisible: true
         });
     }
});

用法:

<div class="ams-items" my-slim-scroll>
</div>

要使用 jQuery,只需确保它在 angular.js 文件之前加载。

有关详细信息,请参阅

【讨论】:

    猜你喜欢
    • 2014-06-24
    • 1970-01-01
    • 2015-06-21
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多