Home > Web Front-end > JS Tutorial > Detailed explanation of the import and export of module modules in AngularJS_AngularJS

Detailed explanation of the import and export of module modules in AngularJS_AngularJS

WBOY
Release: 2016-05-16 15:26:20
Original
1416 people have browsed it

AngularJS is a front-end JS framework from Google. Its core features include: MVC, two-way data binding, instructions and semantic tags, modular tools, dependency injection, HTML templates, and encapsulation of commonly used tools. For example $http, $cookies, $location, etc.

Regarding the import and export of modules in AngularJS, I had not written about it before Bob told me. Thank you Bob for your guidance in this regard and giving me the case code.

In actual AngularJS projects, we may need to put various aspects of a certain field in different modules, and then summarize each module into a file in that field, and then call it from the main module. That’s it:

Above, app.mymodule1, app.mymodule2, app.mymodule are all targeted at a certain field. For example, directive is defined in app.mymodule1, controller is defined in app.mymodule2, and app.mymodule combines app.mymodule1 and app.mymodule2 Summarize them into one place, and then the main module app depends on app.mymodule.

File structure:

mymodule/
.....helloworld.controller.js
.....helloworld.direcitve.js
.....index.js
.....math.js

app.js

index.html

helloworld.controller.js:
var angular = require('angular');
module.exports = angular.module('app.mymodule2', []).controller('HWController', ['$scope', function ($scope) {
 $scope.message = "This is HWController";
}]).name; 
Copy after login

Above, export the module through module.exports and import the module through require.

helloworld.direcitve.js:
var angular=require('angular');
module.exports = angular.module('app.mymodule1', []).directive('helloWorld', function () {
 return {
  restrict: 'EA',
  replace: true,
  scope: {
   message: "@"
  },
  template: '<div><h1>Message is {{message}}.</h1><ng-transclude></ng-transclude></div>',
  transclude: true
 }
}).name; 
Copy after login

Next, put pp.mymodule1 and app.mymodule2 into one place in index.js.

var angular = require('angular');
var d = require('./helloworld.directive');
var c = require('./helloworld.controller');
module.exports = angular.module('app.mymodule', [d, c]).name;
Copy after login

in math.js:

exports = {
 add: function (x, y) {
  return x + y;
 },
 mul: function (x, y) {
  return x * y;
 }
}; 
Copy after login

Finally, reference app.mymodule1:
in app.js

var angular = require('angular');
var mymodule = require('./mymodule');
var math = require('./mymodule/math');
angular.module('app', [mymodule])
 .controller('AppController', ['$scope', function ($scope) {
  $scope.message = "hello world";
  $scope.result = math.add(1, 2);
 }]);
Copy after login

The above is the import and export of module modules in AngularJS shared by the editor. I hope you like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template