2

我正在尝试使用 requirejs 加载 angular 和 jquery。我能做的最好的事情是 50% 的时间一切都正确加载,另一半我得到了错误No Module: mainApp

我假设这会根据 requirejs 异步加载脚本的速度打破一半的时间。

当它工作时,我看到了“Hello World”测试(虽然我确实看到 {{text}} 在它被替换之前闪烁,但我一直在阅读如何解决这个问题)。其余时间我收到错误,{{text}} 只是停留在屏幕上。

Github 回购

Tree:

index.html
- js
    - libs
        require.js
    - modules
        mainApp.js
    main.js

main.js

require.config({
  paths: {
    'jQuery': '//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min',
    'angular': '//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular',
  },
  shim: {
    'angular' : {'exports' : 'angular'},
    'jQuery': {'exports' : 'jQuery'}
  }
});

require(['jQuery', 'angular', 'modules/mainApp'] , function ($, angular, mainApp) {
  $(function () { // using jQuery because it will run this even if DOM load already      happened
    angular.bootstrap(document, ['mainApp']);
  });
});

modules/mainApp.js

define(['angular'], function (angular) {
  return angular.module('mainApp' , []).controller('MainCtrl', ['$scope', function ($scope) {
      $scope.text = 'Hello World';
  }]);
});

Relevant index.html

<head>
    <script src="js/libs/require.js" data-main="js/main"></script>
</head>
<body>
    <div ng-app="mainApp">
        <div ng-controller="MainCtrl">
            {{text}}
        </div>
    </div>
</body>
4

3 回答 3

6

在引导应用程序之前,您可以使用domReady ( UPDATED ) 确保 DOM 已完全加载,即所有 JS 文件都在那里。

requirejs.config({
    paths: {
        jquery: '/path/to/jquery',
        angular: 'path/to/angular',
        domReady: '/path/tp/domReady'
    }, shim: {
        angular: {
            exports: 'angular'
        },  
    }   
});
define(['jquery', 'angular', 'modules/mainApp'], 
    function($, angular, 'mainApp') {
        // tell angular to wait until the DOM is ready
        // before bootstrapping the application
        require(['domReady'], function() {
            angular.bootstrap(document, ['mainApp']);
        }); 
});

有关此问题的更多信息,请参阅RequireJS 官方文档:

当使用 RequireJS 足够快地加载脚本以在 DOM 准备好之前完成它们是可能的。任何尝试与 DOM 交互的工作都应该等待 DOM 准备好。

这个技巧可以在这篇博文中找到。

编辑如果您遵循博客文章的建议,请使用此 domReady 脚本而不是我之前发布的脚本:https ://github.com/requirejs/domReady/blob/master/domReady.js 。

于 2013-11-12T20:14:12.980 回答
2

在 shim 中添加 Jquery 作为 Angular 的依赖项。

require.config({
      paths: {
        'jQuery': '//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min',
        'angular': '//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular',
      },
      shim: {
        'angular' : {'exports' : 'angular', deps: ['jQuery']},
        'jQuery': {'exports' : 'jQuery'}
      }
});
于 2013-11-12T20:07:56.260 回答
1

这可能是一个迟到的回应,但正如达斯汀布莱克所说,您也可以使用 jQuery.ready()而不是包含另一个模块,如domReady.

requirejs.config({
    paths: {
        jquery: '/path/to/jquery',
        angular: 'path/to/angular'
    }, shim: {
        angular: {
            exports: 'angular'
        },  
    }   
});

require(['jquery'], function($) {
    $(document).ready(function(){
        // I like it this way instead of including 
        // everything in the parent function so it's easier to read
        require(['angular', "modules/mainApp"], function(){
         angular.bootstrap(document, ['mainApp']);
        })
    })
});
于 2015-04-09T17:33:52.260 回答