Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.54 KB

controller-as.md

File metadata and controls

50 lines (33 loc) · 1.54 KB

controller-as - disallow assignments to $scope in controllers

You should not set properties on $scope in controllers. Use controllerAs syntax and add data to 'this'. The second parameter can be a Regexp for identifying controller functions (when using something like Browserify)

Rule based on Angular 1.x

Styleguide Reference

Examples

The following patterns are considered problems;

/*eslint angular/controller-as: 2*/

// invalid
angular.module("myModule").controller("SomeController", function($scope) {
    $scope.value = 42;
}); // error: You should not set properties on $scope in controllers. Use controllerAs syntax and add data to "this"

The following patterns are not considered problems;

/*eslint angular/controller-as: 2*/

// valid
angular.module("myModule").controller("SomeController", function($scope) {
    // this for values
    this.value = 42;

    // $scope is fine for watchers
    $scope.$watch(angular.bind(this, function () {
        return this.value
    }), function () {
        // ...
    });
});

Version

This rule was introduced in eslint-plugin-angular 0.1.0

Links