Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

AngularJS Controllers

Author: Sharad Jaiswal
by Sharad Jaiswal
Posted: Sep 06, 2019

AngularJS is a next-generation(ng) javascript framework. AngularsJS extends HTML with some alterations. Learning angularJS is not that much difficult for the beginners if you know HTML and javascript then it will be pretty simple to learn angularJS. Why to use Angular Js 1. AngularJS is an open-source we can use freely. 2. AngularJS is Very usefull to build a single page applications. 3. Code will get reduce in angularJS compare to conventional HTML. 4. We can use MVC concept in AngularJS.clever usage of JSON, client-side caching. 5. AngularJS Easy to learn for the front-end developers. AngularJs ControllerAn application which contains a controller object which is controlled by that controller is called angular controller. All controllers will accept $scope as a parameter which refers to the module that the controller is to control. To understand the above line you need to know the working of angular modules and angular js directives go through once.Use of the controller is to add behavior for the Angular js $scope object. And set up the initial value of the $scope object.   Where we shouldn’t use Angularjs controller :   -Format input — Use angular js form controls instead.   -Filter output — Use angular filters instead.   -Share code or state across controllers — Use Angularjs services instead.   -Manage the life-cycle of other components (for example, to create service instances).            Last Name:

  • Full Name: {{Husband Name + " " + Wife Name}}                     var app = angular.module('firstApp', []);         app.controller('myCtrl', function($scope) {             $scope.Husband Name = "John";             $scope.Wife Name= "Doe";         });            Explanation for the above angularjs controller example   In the above program "myCtrl" function is a javascript function angularjs will add the controller with a $scope object. First of all $scope is an application object. the parameters husband name and wife name is created by the controller called "myCtrl". The directive ng-model is used to bind the values of input boxes to the controller arguments (husbandname, wifename).   Also Read Best
AngularJs Interview Questions for Freshers

Angularjs Controllers in External FileMost of the time we use an external file to store javascript code. For a larger application storing the external file is the best way.

  • To do that just copy the code which is in between            and   .       Then place that in an external file (example myapp.js ) finally include this file in your HTML page look at the below example.

Include the below line to include ng-controller.js file in your application page.          ng-Controller.js file code            angular.module('directives',[])     .directive('example', function () {      return {      //implementation      }      })     .directive('example1', function () {      return {      //implementation      }      });              This is how we can create and use Angular Js Controllers if you have any doubts please feel free to ask.

About the Author

Web Developer at Interview Queries | SEO Analyst at Courseya

Rate this Article
Author: Sharad Jaiswal

Sharad Jaiswal

Member since: Sep 02, 2019
Published articles: 2

Related Articles