English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Moduli AngularJS

AngularJS supporta il metodo modularizzato. I moduli servono a separare la logica (ad esempio servizi, controller, applicazioni) dal codice e mantenere l'ordine del codice. Definiamo i moduli in file js separati e li chiamiamo secondo il nome del file module.js. Nei seguenti esempi, creeremo due moduli-

  • Modulo di applicazione (modulo di applicazione)) - utilizzato per inizializzare l'applicazione.controller(s).

  • Modulo di controller(Modulo di controller) - utilizzato per definire il controller.

Modulo di applicazione

Di seguito è riportato un file chiamato mainApp.js che contiene il seguente codice-

var mainApp = angular.module("mainApp", []);

In questo esempio, utilizziamo la funzione angular.module per dichiarare il modulo di applicazione mainApp e passare un array vuoto. Questo array di solito contiene moduli correlati.

Modulo di controller

studentController.js

mainApp.controller("studentController", function($scope) {
   $scope.student = {
      firstName: "Mahesh",
      lastName: "Parashar",
      fees:500,
      
      subjects:[
         {name:'Physics',marks:70},
         {name:'Chemistry',marks:80},
         {name:'Math',marks:65},
         {name:'English',marks:75},
         {name:'Hindi',marks:67}
      ],
      fullName: function() {
         var studentObject;
         studentObject = $scope.student;
         return studentObject.firstName + " " + studentObject.lastName;
      }
   });
});

In questo esempio, utilizziamo la funzione mainApp.controller per dichiarare il modulo di controller studentController.

Uso dei moduli

<div ng-app="mainApp" ng-controller="studentController">
   ...
   <script src="mainApp.js"></script>
   <script src="studentController.js"></script>
	</div>

In questo esempio, utilizziamo il modulo di applicazione con l'instruzione ng-app e il modulo di controller con l'instruzione ngcontroller. Importiamo mainApp.js e studentController.js nella pagina principale HTML.

Esempi online

Ecco un esempio che mostra l'uso di tutti i moduli menzionati sopra.

testAngularJS.htm

<html>
   <head>
      <title>Angular JS Modules</title>
      <script src="https://cdn.staticfile.org/angular.js/1.3.14/angular.min.js"></script>
      <script src="/run/angularjs/src/module/mainApp.js"></script>
      <script src="/run/angularjs/src/module/studentController.js"></script>
      
      <style>
         table, th, td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
         }
         table tr:nth-child(odd) {
            background-color: #f2f2f2;
         }
         table tr:nth-child(even) {
            background-color: #ffffff;
         }
      </style>
   </head>
   
   <body>
      <h2>AngularJS 模块使用示例</h2>
      <div ng-app="mainApp" ng-controller="studentController">
         
         <table border="0">
            <tr>
               <td>输入名字:</td>
               <td><input type="text" ng-model="student.firstName"></td>
            </tr>
            <tr>
               <td>输入姓氏:</td>
               <td><input type="text" ng-model="student.lastName"></td>
            </tr>
            <tr>
               <td>姓名:</td>
               <td>{{ student.fullName() }}</td>
            </tr>
            <tr>
               <td>科目:</td>
               
               <td>
                  <table>
                     <tr>
                        <th>名称</th>
                        <th>分数</th>
                     </tr>
                     <tr ng-repeat="subject in student.subjects">
                        <td>{{ subject.name }}</td>
                        <td>{{ subject.marks }}</td>
                     </tr>
                  </table>
               </td>
            </tr>
         </table>
      </div>
      
   </body>
</html>
测试看看‹/›

mainApp.js

var mainApp = angular.module("mainApp", []);

studentController.js

mainApp.controller("studentController", function($scope) {
   $scope.student = {
      firstName: "Sea",
      lastName: "Gull",
      fees:500,
      
      subjects:[
             {name:'Fisica',marks:70},
             {name:'Chimica',marks:80},
             {name:'Matematica',marks:65},
             {name:'Inglese',marks:75},
             {name:'Lingua cinese',marks:67}
      ],
      fullName: function() {
         var studentObject;
         studentObject = $scope.student;
         return studentObject.firstName + " " + studentObject.lastName;
      }
   });
});

Risultato di output

Apri il file nel browser webtextAngularJS.htm.