简体   繁体   English

在AngularJS指令中集成jQuery插件调用

[英]Integrating jQuery plugin calls inside AngularJS Directives

I've been working on converting a plain static website into an AngularJS website - just to also improve my skills on that. 我一直在努力将纯静态网站转换为AngularJS网站-只是为了提高我的技能。

So far, I've done routes and looking good so far. 到目前为止,我已经完成了路线,到目前为止看起来还不错。

Now on the home page, I made slider images load using ng-repeat directive and it's also working. 现在在主页上,我使用ng-repeat指令加载了滑块图像,并且该滑块也可以工作。 But I noticed something that the slider itself isn't working its supposed functionality. 但是我注意到一些问题,滑块本身无法正常工作。 So I found out that the jQuery functions I made on a separate js file isn't loading at all. 因此,我发现我在单独的js文件上创建的jQuery函数根本没有加载。 And I also found out that this can be achieved by integrating the plugin calls inside a directive. 而且我还发现,可以通过将插件调用集成到指令中来实现此目的。

So, I made this: 所以,我做到了:

app.directive('featuredSlider', [function() {
return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
        $(elem).owlCarousel({
            itemsCustom: [
                [0, 1],
                [600, 2],
                [1200, 4]
            ],
            autoPlay: 3000
        });
    }
};}]);

I'm using OwlCarousel for the slider and it's not being triggered using the one I did above. 我正在使用OwlCarousel作为滑块,但使用上面的操作并没有触发它。

By the way, this is how my controller look like: 顺便说一下,这就是我的控制器的外观:

app.controller('HomeController', function($scope) {
$scope.featuredImages = []; }

The featuredImages array above has the image URLs for the slider. 上方的featuredImages数组具有滑块的图像URL。

And then, this is the part where the slider is. 然后,这是滑块所在的部分。

<div class = "featured owl-carousel owl-theme featured-slider">
     <div class = "item" ng-repeat = "featured in featuredImages">
       <img ng-src = "{{featured.img}}" />
     </div>
</div>

Anyone who can help me out? 有人可以帮助我吗? I've tried out several methods but still not showing up and working. 我已经尝试了几种方法,但是仍然无法显示和工作。

Thanks in advanced! 提前致谢!

使用指令的控制器并将第三方库作为服务放置,以便您可以将依赖项注入指令控制器中。

Create the dependency in this way: fe: 通过以下方式创建依赖项:fe:

//Import moment.js into your page as always
<script type="text/javascript"
        src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.js"/>

//Before your main app module is declared, declare a momentjs module
angular.module('momentjs',[])
  //And on the momentjs module, declare the moment service that we want
  // available as an injectable
  .factory('moment', function ($window) {
    if($window.moment){
      //Delete moment from window so it's not globally accessible.
      //  We can still get at it through _thirdParty however, more on why later
      $window._thirdParty = $window._thirdParty || {};
      $window._thirdParty.moment = $window.moment;
      try { delete $window.moment; } catch (e) {$window.moment = undefined;
      /*<IE8 doesn't do delete of window vars, make undefined if delete error*/}
    }
    var moment = $window._thirdParty.moment;
    return moment;
  });

and then put the directive in this way to assign their own controller: 然后将指令以这种方式分配给自己的控制器:

 angular.module("example").directive('exampleDirective', [function () {
    return {            
        templateUrl: 'path',
        restrict: 'AE',
        transclude:true,
        controller: 'myDirectiveController'
    };
 }]).controller('myDirectiveController',myDirectiveController);


 function myDirectiveController($scope , $element, $attrs, moment ) {
        //and now you can use this thir party library in the directive controller.
    } 

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

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