Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

ControllerAs Syntax

Ghislain B. edited this page Jul 21, 2015 · 16 revisions

Since version 1.3.36

The library now also support the ControllerAs syntax, so there is no need to specify the $scope anymore, however you will need use the validationService to specify the ControllerAs inside the Angular-Validation global options. It is very important that you declare and set your global validation options before any function declaration else it will fail. For more details on the other available global options, please refer to the Wiki - Global Options

####Directive/Service

app.controller('MyController', function (validationService) {
  var vm = this;

  // declare the Angular-Validation global options BEFORE the functions declaration
  var myValidation = new validationService({ controllerAs: vm });

  // OR you could also do it in 2 lines
  var myValidation = new validationService();
  myValidation.setGlobalOptions({ controllerAs: vm });

  // then after, you can declare your functions
  vm.submit = function () {
     // and use the myValidation Service object previously created outside of this function
     myValidation.checkFormValidity(vm.myform);
});

####This would FAIL The following code would would fail because the Directive already processed the validation before reaching your function and getting the controllerAs is already too late.

app.controller('MyController', ['validationService', function (validationService) {
  var vm = this;

  // then after, you can declare your functions
  vm.submit = function () {
     // and use the myValidation Service object previously created outside of this function
     myValidation.checkFormValidity(vm.myform);

     // calling the controllerAs here would would FAIL, it's too late in the process
     new validationService({ controllerAs: vm }).checkFormValidity(vm.myform);
  }
}]);
Clone this wiki locally