如何通过require js使用Angular模块

时间:2015-10-07 14:25:32

标签: angularjs requirejs

我不熟悉require js动态加载我的js模块。在这里,我想分享我的知识如何通过require js加载我的js模块。

HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
  <!-- data-main attribute tells require.js to load scripts/main.js after require.js loads. -->
  <script data-main="Scripts/main" src="Scripts/require.js"></script>

</head>
<body>
  Test
  <input type="button" id="btnLoadMoudle2" value="Load Module 2" />
</body>
</html>

main.js

// For any third party dependencies, like jQuery, place them in the lib folder.

// Configure loading modules from the lib directory,
// except for 'app' ones, which are in a sibling
// directory.
requirejs.config({
  baseUrl: 'Scripts',
});

// Start loading the main app file. Put all of
// your application logic in there.
//load module1 on page load and show message that module 1 i loaded
requirejs(['module1'], function (module1) {
  module1.Message();

  //attach click event handler that will show load module 2 dynamically at runtime not on page load check this using dom explorer
  document.getElementById('btnLoadMoudle2').addEventListener('click', function () {
     // load module 2 on click and show message
     requirejs(['module2'], function (module2)
    { module2.Message(); });
  }, false);
});

module1.js

//this will be loaded on page load. 
define(function () {
  //Do setup work here

  return {
    Message: function () {
      alert('Module 1 Loaded');
    }
  }
});

module2.js

//this will be loaded on button click
define(function () {

  return {
    Message: function () {
      alert('Module 2 Loaded');
    }
  }
});

现在我的问题是如何使用require.js加载角度多模块和依赖项。我是棱角分明的新人。所以我正在寻找一个简单的示例代码来开始。

0 个答案:

没有答案