3-Angular的模組化

來源:互聯網
上載者:User

標籤:

在前面的幾個文章中,我們的資料都是以函數的方式定義在全域的範圍下,這樣很顯然是不對的,沒有模組化的思想,下面的例子介紹Angular的模組化!

 

<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>AngularJs模組化</title><script type="text/javascript"src="https://code.angularjs.org/1.3.8/angular.min.js"></head><body><div ng-controller=‘Aaa‘>    <p>{{name}}</p></div><div ng-controller=‘Bbb‘>    <p>{{name}}</p></div><script type="text/javascript">var m1 = angular.module(‘myApp‘,[]);
//參數1:模組的名字,參數2:當前模組中依賴的模組,後面的文章會介紹m1.controller(
‘Aaa‘,function($scope){ $scope.name = ‘hello AngularJs‘;});m1.controller(‘Bbb‘,function($scope){ $scope.name = ‘Hi‘;});
</script></body></html>

首先在html上綁定初始化的名稱,並附上myApp,使用angular.model在JS中定義多個模組,並附上參數。

再使用控制器定義資料,注意控制器傳遞的參數,模組名和匿名函數。

都知道我們的項目在上線後合并壓縮的,這樣我們匿名函數傳遞的形參在壓縮後出現問題,所以這個時候我們需要將參數包裹成數組,來避免壓縮的問題!

 

m1.controller(‘Aaa‘,[‘$scope‘,function($scope){    $scope.name = ‘hello AngularJs‘;}]);m1.controller(‘Bbb‘,[‘$scope‘,function($scope){    $scope.name = ‘Hi‘;}]);

 

 

 

學習筆記,如有不足,請指正!轉載請保留原文連結,謝謝。

最後,微博求粉,謝謝。

 

3-Angular的模組化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.