requiredDirective = function() {
  return {
    require: '?ngModel',
    link: function(scope, elm, attr, ctrl) {
      if (!ctrl) return;
      attr.required = true; // force truthy in case we are on non input element

      var validator = function(value) {
        if (attr.required && (isEmpty(value) || value === false)) {
          ctrl.$setValidity('required', false);
          return;
        } else {
          ctrl.$setValidity('required', true);
          return value;
        }
      };

      ctrl.$formatters.push(validator);
      ctrl.$parsers.unshift(validator);

      attr.$observe('required', function() {
        validator(ctrl.$viewValue);
      });
    }
  };
}
@ngdoc directive
@name ng.directive:ngChange
@restrict E

@description
Evaluate given expression when user changes the input.
The expression is not evaluated when the value change is coming from the model.

Note, this directive requires `ngModel` to be present.

@element input

@example
<doc:example>
  <doc:source>
    <script>
      function Controller($scope) {
        $scope.counter = 0;
        $scope.change = function() {
          $scope.counter++;
        };
      }
    </script>
    <div ng-controller="Controller">
      <input type="checkbox" ng-model="confirmed" ng-change="change()" id="ng-change-example1" />
      <input type="checkbox" ng-model="confirmed" id="ng-change-example2" />
      <label for="ng-change-example2">Confirmed</label><br />
      debug = {{confirmed}}<br />
      counter = {{counter}}
    </div>
  </doc:source>
  <doc:scenario>
    it('should evaluate the expression if changing from view', function() {
      expect(binding('counter')).toEqual('0');
      element('#ng-change-example1').click();
      expect(binding('counter')).toEqual('1');
      expect(binding('confirmed')).toEqual('true');
    });

    it('should not evaluate the expression if changing from model', function() {
      element('#ng-change-example2').click();
      expect(binding('counter')).toEqual('0');
      expect(binding('confirmed')).toEqual('true');
    });
  </doc:scenario>
</doc:example>