使用requirejs來管理angularJS依賴樣本

來源:互聯網
上載者:User

標籤:分享   views   gravity   src   檔案   定義   app   doctype   知識庫   

有關requirejs是什麼在這裡不做解釋,只用純程式碼實戰讓你感受requirejs依賴管理的強大。

一、首先要先下載require.js,然後整一個入口檔案main.js包括了對其他js的引用。

[javascript] view plain copy print?
  1. //配置依賴  
  2. require.config({  
  3.     paths: {  
  4.       "angular":"angular"  
  5.     },  
  6.     shim:{  
  7.         "angular": {  
  8.             "deps":[],  
  9.             "exports": "angular"  
  10.         }  
  11.     }  
  12.   });  
  13.   
  14. //定義模組  
  15. define([  
  16.     ‘angular‘,  
  17.     ‘controller‘  
  18.     ], function (angular) {      
  19.     
  20.     angular.module(‘HelloModel‘, [‘HelloCtrls‘]);  
  21.   
  22.     return {  
  23.         angularModules: [ ‘HelloModel‘ ]  
  24.     };      
  25.       
  26. });  



二、定義angular中的控制器模組controller.js

[javascript] view plain copy print?
  1. define([ ‘angular‘ ], function(angular) {  
  2.   
  3.     var componentCtrls = angular.module(‘HelloCtrls‘, []);  
  4.   
  5.      componentCtrls.controller(‘helloCtrl‘, [ ‘$scope‘, function($scope) {  
  6.         $scope.greet = "hello world";  
  7.       } ]);  
  8.   
  9.     return componentCtrls;  
  10. });  



三、建立一個index.html檔案測試

 

[html] view plain copy print?
  1. <!DOCTYPE html>  
  2. <html ng-app="HelloModel">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>Insert title here</title>  
  6.   
  7. <script src="js/require.js" data-main="js/main" defer async="true" ></script>  
  8. </head>  
  9. <body ng-controller="helloCtrl">  
  10.   
  11. {{greet}}  
  12.   
  13. </body>  
  14. </html>  



四、檔案組織圖

    

使用requirejs來管理angularJS依賴樣本

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.