The Module in AngularJS

AngularJS supports module-oriented approach. The module is used to distinguish the logic, service and application processing . and make the code clear. We define modules into separate JavaScript files and name them according to each module.js file. In this chapter, we will create 2 modules:

  1. Application Module - used to initialize the application with the controller.
  2. Controller Module - used to define controller.

Application Module in AngularJS

ungdungAngularjs.js

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

Above we declare the Application Module ungdungAngularjs using the modular function of AngularJS. We initialize an empty array for it. This array usually contains independent components in the module.

Controller Module in AngularJS

sinhvienController.js

 ungdungAngularjs . controller ( "sinhvienController" , function ( $ scope ) { $ scope . sinhvien = { cough : "Quan Tri" , ten : "Bring" , hocphi : 200 , tenMonHoc : [ { ten : 'Vat Ly Dai Cuong' , diemthi : 8.5 }, { ten : ' Tri Hoc' , diemthi : 7.5 }, { ten : 'Toan' , diemthi : 9.0 }, { ten : 'Tieng Anh' , diemthi : 8.0 }, { ten : 'Kinh Te' , diemthi : 9.5 } ], hoten : function () { var doitongngien ; doituuonginhvien = $ scope . Return ; return doituuongvien . Ho + "" + doituongsinhvien . Ten ; } }; }); 

Here we declare a module as sinhvienController using the ungdungAngularjs.controller function.

Use the Module in AngularJS

 ng-app = "ungdungAngularjs" ng-controller = "sinhvienController" > .  src = "ungdungAngularjs.js" >  src = "sinhvienController.js" > 

Here, we used Application Module using the ng-app directive and Controller Module using the ng-controller directive. We include both ungdungAngularjs.js and sinhvienController.js in the HTML file.

For example:

Here is an example to illustrate how to use the two above Modules in the following AngularJS application:

viduModule.html

 Module in AngularJS  src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" >  src = "ungdungAngularjs.js" >  src = "sinhvienController.js" > 

 

 Cavalier AngularJS  ng-app = "ungdungAngularjs" ng-controller = "sinhvienController" >  border = "0" > Nhap ho:  type = "text" ng-model = "sinhvien.ho" > Nhap ten:  type = "text" ng-model = "sinhvien.ten" > Ho and Ten: {{sinhvien.hoten ()}} Mon:   Ten   Exam scores  ng-repeat = "printable version of the account.tenMonHoc" > {{monhoc.ten}} {{monhoc.diemthi}} 

ungdungAngularjs.js

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

sinhvienController.js

 ungdungAngularjs . controller ( "sinhvienController" , function ( $ scope ) { $ scope . sinhvien = { cough : "Quan Tri" , ten : "Bring" , hocphi : 200 , tenMonHoc : [ { ten : 'Vat Ly Dai Cuong' , diemthi : 8.5 }, { ten : ' Tri Hoc' , diemthi : 7.5 }, { ten : 'Toan' , diemthi : 9.0 }, { ten : 'Tieng Anh' , diemthi : 8.0 }, { ten : 'Kinh Te' , diemthi : 9.5 } ], hoten : function () { var doitongngien ; doituuonginhvien = $ scope . Return ; return doituuongvien . Ho + "" + doituongsinhvien . Ten ; } }; }); 

Result

Open the viduModule.html page in the browser. Here are the results.

The Module in AngularJS Picture 1

Follow tutorialspoint

Previous article: HTML DOM element in AngularJS

Next article: Form in AngularJS

4 ★ | 1 Vote

May be interested

  • Module in Node.jsModule in Node.js
    node.js uses the module architecture to simplify the creation of complex applications. the module is like libraries in c, c #, java ... each module contains a set of functional functions related to an object of module.
  • Multilingual (i18n) in AngularJSMultilingual (i18n) in AngularJS
    angularjs provides multi-lingual functions (i18n) with 3 types of filters, currency, date and number. we just need to combine the js corresponding to the location depending on the country. by default it will attach to the location in the web browser.
  • Expression in AngularJSExpression in AngularJS
    expression is used to bind application data to html tags. expression is written in {{expression}}. expression has a similar way of operating as the ng-bind directive.
  • Controller component in AngularJSController component in AngularJS
    an angularjs application works primarily based on the controller component to control the flow of data in the application.
  • MVC structure in AngularJSMVC structure in AngularJS
    model view controller or mvc is a popular call, a software design model for web-based applications.
  • Create the table in AngularJSCreate the table in AngularJS
    table data is often repeated in natural order. in angularjs, ng-repeat directive can be used to draw tables easily. below is an example of how to use ng-repeat to create tables.
  • Utility Module in Node.jsUtility Module in Node.js
    the node.js library module provides many utility modules that help you a lot while developing applications on node.js. the following table lists and describes these utility modules.
  • Scope in AngularJSScope in AngularJS
    scope is a special javascript object with the role of linking controllers and views. scope contains information as model data. in the controller, model data can be accessed via the $ scope object.
  • What is Hardware Security Module (HSM)? Why is it so important?What is Hardware Security Module (HSM)? Why is it so important?
    the management of cryptographic keys, including generating, storing, and verifying them, is often a major obstacle to system security. the good news is that you can securely manage cryptographic keys using the hardware security module (hsm).
  • Module time in PythonModule time in Python
    python has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!