Home >Web Front-end >JS Tutorial >Understanding 'modules' in Angularjs

Understanding 'modules' in Angularjs

青灯夜游
青灯夜游forward
2021-01-18 18:39:212286browse

This article will take you to understand the "module" in angularjs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Understanding 'modules' in Angularjs

Related tutorial recommendations: "angularjs tutorial"

The module defines an application.
Modules are containers for different parts of your application.
Modules are containers for application controllers.
Controllers usually belong to a module.

1 Create a module

You can create a module through the angular.module function of AngularJS:

<div ng-app="myApp">...</div>

<script>

    var app = angular.module("myApp", [&#39;其他模块&#39;]);

</script>

The "myApp" parameter corresponds to the HTML element that executes the application.
Now you can add controllers, directives, filters, etc. in your AngularJS application.

2 Add a controller

You can use the ng-controller directive to add an application controller:

<div ng-app="myApp" ng-controller="myCtrl">
    {{ firstName + " " + lastName }}
</div>

<script>

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

    app.controller("myCtrl", function($scope) {
        $scope.firstName = "John";
        $scope.lastName = "Doe";
    });

</script>

3 Add command

<div ng-app="myApp" runoob-directive></div>

<script>

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

    app.directive("runoobDirective", function() {
        return {
            template : "我在指令构造器中创建!"
        };
    });
</script>

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Understanding 'modules' in Angularjs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete