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

Talent Mapping for Strategic Growth: 7 Key Benefits

The process of talent mapping involves creating a comprehensive and detailed analysis of future industry…

3 days ago

10 Proven SEO Strategies to Boost Your Google Rankings in 2026

Introduction: The digital landscape is always evolving, and if there is one thing you can…

6 days ago

Generative AI for Beginners: Introduction to AI

Forget all the big fancy words and tech hype around “Artificial Intelligence” for now. The…

6 days ago

How Does Shade and Bad Weather Impact Solar Panels?

Solar panels work by converting the solar energy received from the sun into usable electric…

1 week ago

Websites Design Companies: How They Help Businesses Succeed Online

In today’s digital-first economy, your website is often the first impression your business makes. Whether…

2 weeks ago

Best Social Media Agencies in Dubai – 2025 Guide

Dubai’s digital landscape is highly competitive, making it essential for businesses to work with the…

2 weeks ago