We can communicate with HTML DOM using $scope variable in AngularJS, but it’s not hard and fast rule to use scope to communicate with View, we can also create controller without using $scope and use all the properties and behavior in view (HTML).
Why scope-less controller
Sometimes controller become complex by using $scope for providing data and behavior to view, in that situation we can use scopeless controller.
But if you have designed your AngularJS application perfectly, there is no need to go for scopeless controllers.
Creating scope-less controller
- angular module(app.js):
- angular.module('myApp', []);
- var app = angular.module("myApp");
- app.controller("myController", function ()
- {
- this.title = 'scopeless Controller Test';
- this.name = 'Anupam';
- this.sayHello = function ()
- {
- alert('Hello ' + this.name);
- }
- });
I would love to explain this here but I am still in a way to explore what is this in JavaScript.
Here is how we can get data from controller to view.
Binding Data to View using scope-less controller
View (index.html)
- <!DOCTYPE html>
- <html ng-app="myApp" ng-controller="myController as ctrl">
- <head>
- <script src="Scripts/angular.min.js"></script>
- <script src="app/app.js"></script>
- <script src="app/homeController.js"></script>
- <link href="Css/bootstrap.min.css" rel="stylesheet" />
- <title>{{ctrl.title}}</title>
- </head>
- <body>
- <nav role="navigation" class=" navbar navbar-default">
- <div class="navbar-header">
- <a href="#" class="navbar-brand">
- {{ctrl.title}}
- </a>
- </div>
- </nav>
- <div class="container body-content">
- <div class="col md-6">
- <div class="row">
- <div class="well-lg">
- Hi {{ctrl.name}}
- </div>
- </div>
- <div class="row">
- <div class="well-lg">
- <input type="button" ng-click="ctrl.sayHello()" value="Say Hello" class="btn" />
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Note: We have to create a variable using as keyword, since we can’t access controller using its name. For example: we can’t access name property using myController.name in view.
Summary
In this article we learned how we can communicate between view and controller without using $scope variable. I have also attached sample code if you wish to download. Hope you enjoyed this.

MS TeamPosted Jun 18, 2016, 8:27 AM
Hi sir, Nice Topic thanks for sharing .But can you suggest me how we can handle custom error in angular js (Like 401) and handling session in angular js.
Sonu ChaudharyPosted Mar 18, 2016, 2:38 AM
nice share
Santhakumar MunuswamyPosted Oct 25, 2015, 11:39 AM
Good one
Anupam SinghPosted Oct 25, 2015, 2:26 AM
Thanks all.
Debasis SahaPosted Oct 23, 2015, 8:30 AM
Nice one
Nilesh JadavPosted Oct 23, 2015, 8:22 AM
Good one sir !
Sibeesh VenuPosted Oct 23, 2015, 1:36 AM
Nice Share
Mukesh KumarPosted Oct 22, 2015, 2:46 PM
Good One
Priyaranjan K SPosted Oct 22, 2015, 12:25 PM
Nice Share