AngularJS Modular Detailed and example code

Source: Internet
Author: User
Angularjs has several major features, such as:

1 MVC

2 modularity

3 instruction System

4 Bidirectional data binding

Then this article will take a look at the Angularjs modularity.

First of all, let's talk about why modularity is possible:

1 Increased reusability of modules

2 Customizing the load order by defining the module

3 in unit tests, you do not have to load all the content

In a few examples, the controller's code is written directly inside the script tag, so that the declared function is global and is obviously not the best choice.

Here's a look at how to modularize:

<script type= "Text/javascript" >     var myappmodule = angular.module (' myApp ', []);           Myappmodule.filter (' Test ', function () {       return function (name) {         return ' Hello, ' +name+ '! ';       };     });      Myappmodule.controller (' Myappctrl ', [' $scope ', function ($scope) {       $scope. name= ' Xingoo ';     }]);   </script>

First, create the module with the global variable angular myappmodule

Angular.module (' myApp ', []);

The first parameter is the binding app name, which identifies the entry point of the angular in the page, similar to the function of main.

The second parameter [] identifies the dependent module.

Let's see how to use the module now!

<!doctype html>

Directly bind MyApp to Ng-app, it is possible.

In script, we created a filter and a controller through the module.

The role of filter is to add string adornments.

The function of the controller is to initialize the variable.

The running results of the program are as follows:

The above is the ANGULARJS modular data collation, follow-up continue to supplement the relevant information, thank you for the support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.