AngularJS modularization details and instance code, angularjs Modularization

Source: Internet
Author: User

AngularJS modularization details and instance code, angularjs Modularization

AngularJS has several major features, such:

1 MVC

2 Modular

3 Command System

4. bidirectional data binding

In this article, we will look at the modularity of AngularJS.

First, let's explain why modularization is required:

1. added the reusability of modules.

2. Customize the loading sequence by defining the module

3. In unit testing, you do not have to load all content.

In the previous examples, the Controller code is directly written in the script tag. The declared functions are global and are obviously not the best choice.

Let's take a look at how to modularize it:

 <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 myAppModule using the global variable angular.

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

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

The second parameter [] identifies the dependent module.

Let's take a look at how to use the module!

<!doctype html>

Bind the myApp directly to the ng-app.

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

Filter is used to add string modifier.

The controller is used to initialize variables.

The program running result is as follows:

The above is the modular information of AngularJS. We will continue to add relevant information in the future. Thank you for your support for this site!

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.