ANGULARJS Modular detailed and instance code _ANGULARJS

Source: Internet
Author: User
Tags script tag

Angularjs has several major features, such as:

1 MVC

2 Modular

3 instruction System

4 Bidirectional data binding

Then this article will look at the Angularjs of the module.

First of all, why do you want to achieve modularity:

1 increases the reusability of the module

2 Customize the load sequence by defining the module

3 in unit tests, you don't have to load all the content

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

Let's 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 through the global variable angular myappmodule

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

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

The second parameter [] identifies the dependent module.

Let's see how to use the module!

<!doctype html>
 
 

Direct binding MyApp to the Ng-app, it's OK.

 In script, we create a filter and a controller from the module.

The role of filter is to add string adornments.

The role of the controller is to initialize the variable.

The results of the program's operation are as follows:

The above is the ANGULARJS modular data collation, follow-up continue to supplement the relevant information, thank you for your 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.