Categories: Javascript

AngularJS – Different Ways to Declare Controllers

The article represents various ways of declaring a controller along with code examples. Please feel free to comment/suggest if I have missed on any key aspects.Following are two different ways in which controllers can be declared:
  • Using “as aliasName” in ng-controller. Example: ng-controller=”HelloCtrl as ctrl”
  • Just the controller name. In this strategy, $scope object is bound to data and methods. Example: ng-controller=”HelloCtrl”

 

Using “as aliasName” in ng-controller (Recommended Way)

Using “as aliasName” specifier when defining the controller in ng-controller directive. The methods and properties can be bound on to the controller using “this” keyword. There are benefits of binding properties directly to the controller and thus, it is the recommended way (best practice) to declare the controller using “as alias” method. Following are some of the benefits:

  • In case of multiple controllers, one knows which controllers are accessed in reading the template.
  • Enhanced readability, in general
  • Helps create controller using prototypal inheritance.

Following is how view code looks like. Note the “as ctrl” and then, “ctrl.name”

<div ng-controller="HelloCtrl as ctrl">
    <input type="text" name="name" ng-model="ctrl.name"/>
</div>

Following is how the controller looks like:

angular.module( "helloApp", [] )
    .controller( "HelloCtrl", function() {
        this.name = '';
    }]);

 

Injecting the Scope Object into the Controller (Rookies Way)

This is most common and popular way of working with the controller. For rookies to quickly get on board, this may be the recommended way.

In this strategy, the properties and methods get bound as scope properties and methods and can be later accessed on scope object within the controller object. This strategy requires $scope object to be injected.

Following is how view code looks like. Note the absence of “as” in the ng-controller directive and “ctrl.” alias in ng-model directive.

<div ng-controller="HelloCtrl">
    <input type="text" name="name" ng-model="name"/>
</div>

Following is how the controller looks like. Notice that $scope is injected and the model is bound to $scope object.

angular.module( "helloApp", [] )
    .controller( "HelloCtrl", [ '$scope', function( $scope ) {
        $scope.name = '';
    }]);

[adsenseyu1]

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

View Comments

  • another way is registering a controller using a $controllerProvider. Then use the $controller service to create it. You can pass the $scope as parameters to $controller service.

Share
Published by
Ajitesh Kumar
Tags: angularjs

Recent Posts

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

2 days ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

3 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

6 days ago

Loss Function vs Cost Function vs Objective Function: Examples

Last updated: 19th April, 2024 Among the terminologies used in training machine learning models, the…

1 week ago

Model Parallelism vs Data Parallelism: Examples

Last updated: 19th April, 2024 Model parallelism and data parallelism are two strategies used to…

1 week ago

Model Complexity & Overfitting in Machine Learning: How to Reduce

Last updated: 4th April, 2024 In machine learning, model complexity, and overfitting are related in…

3 weeks ago