<commit_before>angular.module('ash', []).directive("ngTypeahead", function () {

  return {
    restrict: 'A',
    require: '?ngModel',
    scope: {
      options: '=',
      datasets: '='
    },
    link: function ($scope, element, attrs, ngModel) {

      $scope.isChanged = false;

      element.typeahead($scope.options || {}, $scope.datasets || {});

      ngModel.$parsers.push(function (value) {
        ngModel.$setValidity('typeahead', $scope.isChanged);

        if ($scope.isChanged) {
          $scope.isChanged = false;
        } else {
          value = undefined;
        }

        return value;
      });

      $scope.changeModel = function(value) {
        $scope.$apply(function () {
          $scope.isChanged = true;
          ngModel.$setViewValue(value);
        });
      }

      element.bind('typeahead:selected', function(event, suggestion) {
        $scope.changeModel(suggestion);
      });

      element.bind('typeahead:autocompleted', function(event, suggestion, dataset) {
        $scope.changeModel(suggestion);
      });

    }
  }

});<commit_msg>fix undefined model value for external set up<commit_after>angular.module('ash', []).directive("ngTypeahead", function () {

  return {
    restrict: 'A',
    require: '?ngModel',
    scope: {
      options: '=',
      datasets: '='
    },
    link: function ($scope, element, attrs, ngModel) {

      $scope.isChanged = false;

      element.typeahead($scope.options || {}, $scope.datasets || {});

      ngModel.$parsers.push(function (value) {
        ngModel.$setValidity('typeahead', $scope.isChanged);

        if ($scope.isChanged) {
          $scope.isChanged = false;
        } else {
          var key = $scope.datasets.displayKey ? displayKey : 'value';
          if (value == ngModel.$modelValue[key]) {
            value = ngModel.$modelValue;
          } else {
            value = undefined;
          }
        }

        return value;
      });

      $scope.changeModel = function(value) {
        $scope.$apply(function () {
          $scope.isChanged = true;
          ngModel.$setViewValue(value);
        });
      }

      element.bind('typeahead:selected', function(event, suggestion) {
        $scope.changeModel(suggestion);
      });

      element.bind('typeahead:autocompleted', function(event, suggestion, dataset) {
        $scope.changeModel(suggestion);
      });

    }
  }

});
{'fraction_non_alphanumeric': 0.1556761268781302, 'fraction_numerical': 0.0, 'mean_word_length': 2.247967479674797, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1495803', 'n_tokens_mistral': 725, 'n_tokens_neox': 696, 'n_words': 116}