Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can be the reason to use "controllerAs" property?

Tags:

angularjs

I am working on an AngularJS tutorial, and I see the following code:

.state('index',{
  url:"/",
  templateUrl:"views/index.html",
  controller:"IndexCtrl",
  controllerAs:"index"
})

What is the reason someone would want to use the controllerAs property?

like image 387
Michael Avatar asked Aug 05 '15 16:08

Michael


People also ask

Why do controllers need to be aware of the view?

However, because controllers support the dependency injection system in AngularJS, and simple model objects do not, controllers often leverage services to help build up the model, or to handle interactions from the view. In order to prepare the model for the view, the controller needs to be aware of the view.

Why do we use controlleras in angular?

In fact, it’s a best practice to use controllerAs throughout our Angular apps in order to prevent some common problems that developers run into fairly often.

Can we use controller as a template in a directive?

And as you probably know, directives can also have controllers and yes, we can use controllerAs there too. app.controller ('SomeController', function () { this.foo = 'bar'; }); app.directive ('someDirective', function () { return { restrict: 'A', controller: 'SomeController', controllerAs: 'ctrl', template: ' { {ctrl.foo}}' }; }); Great.

What is the use of controller parameter in route?

When using routes, the controllerAs parameter is passed to the “when” or “otherwise” function to provide the name that will be used in the view to refer to the controller. The views used with the route would look identical to the previous example without the controller directive.


Video Answer


2 Answers

Few things:

1. Reduce scope usage

Instead of loading every data in the scope of a controller, you could simply use this to load up everything that you require.

eg:

Route

state('index',{
    url:"/",
    templateUrl:"views/index.html",
    controller:"ListCtrl",
    controllerAs:"list"
})

In Controller

angular.module('feed').controller('ListCtrl', function($scope, reddit){
    var vm   = this;
    vm.names = ["Michael", "Roy"];

});

In Template:

<ul>
    <li ng-repeat="name in list.names">
        <div>{{name}}</div>
    </li>
</ul>

2. Correct scope usage

When multiple controllers come into play, scope becomes a tricky thing. Using controllerAs method will go a long way is resolving this. An example is shown below:

Wrong:

<span>Outside Controller: Your name is: {{username}}</span>
<div ng-controller="SignupController">
    <span>Inside Controller: Your name is: {{username}}</span>
    <fieldset legend="User details">
        <input ng-model="username">
    </fieldset>
</div>

Correct:

<span>Outside Controller: Your name is: {{user.name}}</span>
<div ng-controller="SignupController">
    <span>Inside Controller: Your name is: {{user.name}}</span>
    <fieldset legend="User details">
        <input ng-model="user.name">
    </fieldset>
</div>

Found an image which makes everthing more clear:

enter image description here

Courtesy : AngularJs "controller as" syntax - clarification?

like image 171
Roy M J Avatar answered Sep 17 '22 21:09

Roy M J


Yes. A little more info:

Before the controllerAs syntax, methods and properties needed to be exposed to views by binding them to the $scope. With controllerAs, your controller instance is bound to the $scope as the property you select.

This way you can use Plain Old JavaScript Classes for your controllers.

Editorial: This is a much cleaner approach to development. One of the things that makes Angular so easy to write tests for is that your Controllers and components do not need to inherit from framework base-classes. See Backbone and Ember.

So with the old style your controllers would look like (in ES6 for simplicity):

YourController.$inject = ['$scope'];
class YourController {
  
    constructor($scope) {

        $scope.myMethod = () => { . . . };

        $scope.myProperty = true;
    }
}

With the controllerAs

class YourController {
  
    constructor() {

        this.myProperty = true;
    }

    myMethod() { . . . };
}

Just a plain old class rather than decorating or monkeypatching the $scope.

like image 30
Martin Avatar answered Sep 18 '22 21:09

Martin