繁体   English   中英

jQueryUI + RequireJS + AngularJS引发异常

[英]jQueryUI + RequireJS + AngularJS throws exceptions

我将jQueryUI与RequireJS和AngularJS结合使用。 我将jqueryui组件包装在require语句中,例如:

define(['jquery','./core','./mouse', './widget'], function (jQuery) {
    (function( $, undefined ) {

         $.widget("ui.draggable", $.ui.mouse, {....});
    })(jQuery);

});

并创建了一个AngularJS指令将其包装为:

 require(['angular', 'app', 'jquery', 'lib/jquery-ui/draggable'], function(angular, app, $){

    app.directive('draggable', ['$timeout','draggableConfig', function ($timeout) {
      return {
        restrict: 'AE',
        scope: {
            ngModel: '=',
            options: '='
        },
        link: function ($scope, $element, $attributes) {
            $timeout(function(){
                $element.draggable();
            }, 50)
        }
    }
   }]);
});

但每5次应用中有2次抛出错误,例如:

TypeError: Object [object Object] has no method 'draggable'
at http://localhost/phoenix/directives/draggable.js:21:30
at http://localhost/phoenix/lib/angular/angular.js:13152:28
at completeOutstandingRequest (http://localhost/phoenix/lib/angular/angular.js:3996:10)
at http://localhost/phoenix/lib/angular/angular.js:4302:7 

我尝试了无数次尝试,但始终没有运气。 我非常确定,可拖动对象不会受指令的加载时间绑定到$,但是依赖项是正确的,所以我不知道为什么。 有什么想法吗?

这是一个时间问题。 RequireJs异步加载文件。 您需要在require config中使jquery-ui成为jquery的依赖项。

require.config({
    baseUrl: 'Scripts/App',
    paths: {
        jQueryUI: '../Lib/Jquery/jquery-ui.min',
        jQuery: '../Lib/Jquery/jquery.min'        
    },
    shim: {
        jQueryUI: { deps: ['jQuery'] },
    }
});

柱塞

该指令在应使用define时具有require

暂无
暂无

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

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