angularjscontrolleras

When using the angularjs "controllerAs" syntax is it still good practice to have a container object?


I've recently found the controllerAs syntax and I'm wondering if it precludes the need for wrapping your scope variables in objects in order to get the reference.

Sorry I'm probably not asking clearly but what I mean is are both of the following declarations as safe as each other or should I still use a model object when using controllerAs to avoid issues with nested scopes?

Old method -

$scope.model = {
    myData: "test"
}

<div ng-controller="myController">
    <input ng-model="model.test" />
</div>

ControllerAs -

$scope.myData = "test"

<div ng-controller="myController as myC">
    <input ng-model="myC.test" />
</div>

Solution

  • No, the container is no longer necessary, one of the main purposes of controllerAs syntax is to introduce this useful pattern, which helps to eliminate prototype inheritance effects in scope when they are undesirable (i.e. almost always).

    Scope prototype inheritance can still be reached if necessary by using $scope instead of this in controller.