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!