0

UPDATE moved to code review

I am looking for an elegant way of attaching a helper to my angular controller. I have followed this post and made some slight modifications to take $scope into account. This is what I have so far:

Here is my helper:

var productForm = {
  helpers: {
    setup: function($scope) {
      this.$scope = $scope;
    },
    isInvalid: function(ngModel) {
      return this.$scope.productForm[ngModel].$invalid && !this.$scope.productForm[ngModel].$pristine;
    }
  }
};

In my controller, I am doing the following:

intakeApp.controller("MageProductCtrl", ["$scope", "$http", function($scope, $http) {

  $scope.helpers = productForm.helpers;
  $scope.helpers.setup($scope);
  ...
}

in my view, I am referencing the helper as such:

<span class="help-block" ng-show="helpers.isInvalid('designer')">can't be blank</span>

Any room for improvement?

Community
  • 1
  • 1
dipole_moment
  • 5,266
  • 4
  • 39
  • 55

0 Answers0