DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
AngularJS

AngularJS Modules

Share

Modules are used to seperate logics and keep the code clean. So we can define modules in separate js file. Here we will create two modules i.e. Controller Module and Application Module.

Controller Module: controller.js
Here we are declaring a controller userController module using myApp.controller function.

 myApp.controller("userController", function($scope) {
 $scope.user = {
 first_name: "Tim",
 last_name: "Shawn",
 full_name: function() {
 var userObj;
 userObj = $scope.user;
 return userObj.first_name + " " + userObj.last_name;
 }
 };
 });

Application Module : module.js

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

Here we declare myApp module using angular.module function. We are passing an empty array to it. Generally this array contains dependent modules.

Html:

 <div ng-app="myApp" ng-controller="userController">
 <table border="0">
 <tr><td>First name:</td><td><input type="text" ng-model="user.first_name"></td></tr>
 <tr><td>Last name: </td><td><input type="text" ng-model="user.last_name"></td></tr>
 <tr><td>Full name: </td><td>{{user.full_name()}}</td></tr>
 </table>
 </div>

Following example shows the full code of AngularJS Modules:

<html>
 <head>
 <title>AngularJS Modules</title>
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
 <script src="module.js"></script>
 <script src="controller.js"></script>
 </head>
 <body>
 <h2>AngularJS Modules</h2>
 <div ng-app="myApp" ng-controller="userController">
 <table border="0">
  <tr><td>First name:</td><td><input type="text" ng-model="user.first_name"></td></tr>
  <tr><td>Last name: </td><td><input type="text" ng-model="user.last_name"></td></tr>
  <tr><td>Full name: </td><td>{{user.full_name()}}</td></tr>
 </table>
 </div>
 </body>
</html>
Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

View Comments

Recent Posts

Writing Tools That Help Content Marketers Produce Cleaner Work

Content marketing has a hidden tax. It's not the writing itself, it's everything that happens…

3 days ago

How Data Analytics Is Actually Driving Smarter Marketing Decisions

Most marketing teams aren't failing because they lack data. They're failing because they can't act…

2 weeks ago

The Ultimate SPF Tester Guide: Boost Email Deliverability In Minutes

Email marketing continues to be one of the most effective ways for businesses to communicate,…

2 weeks ago

Sales Ops: A role that helps sales teams work as efficiently as possible

Xerox first introduced it around the mid-1970s. The need came up because the management activities…

3 weeks ago

Forex Investment Tips for Beginners

Investing in the forex market may look to be a dangerous game. With some worthwhile…

3 weeks ago

How Did The Restaurant Industry change In Post Pandemic Era?

The coronavirus outbreak has drastically changed the way we live our lives. Yes, that's absolutely…

1 month ago