title: Rewriting ng-options with ng-repeat so it works with md-select
body: <p>I am trying to transform a Bootstrap select field into Angular Material, but am having difficulty getting the code to work with ng-repeat, instead of ng-options.  The original code looks like this:</p>
  
  <pre><code>&lt;select name="{{field.name}}" ng-model="fieldValue" ng-model-options="{getterSetter: true}" sn-select-width="auto" ng-disabled="field.isReadonly()" ng-options="c.value as c.label for c in field.choices track by c.value"&gt;
  </code></pre>
  
  <p>The new AngularJS Material html code looks like this:</p>
  
  <pre><code>&lt;md-select name="{{field.name}}" ng-model="fieldValue" ng-disabled="field.isReadonly()"&gt;
      &lt;md-option ng-value="c.value" ng-repeat="c in field.choices | filter:searchTerm track by c.value"&gt;{{c.label}}&lt;/md-option&gt;
  &lt;/md-select&gt;
  </code></pre>
  
  <p>The selected item from md-select won't save in the back-end table.  What am I missing here?  I am pretty sure not being able to use ng-options is causing this issue, but how do I fix it with ng-repeat?</p>
  
  <p>UPDATE:  The rest of the out of the box directive code is below for reference.</p>
  
  <pre><code>link: function(scope, element, attrs, ngModel) {
              scope.clearSearchTerm = function() {
                  scope.searchTerm = '';
              };
  
              var g_form = scope.getGlideForm();
              var field = scope.field;
              var fieldOptions;
              var isOpen = false;
               scope.fieldValue = function() {
                   return field.value;
               };
              g_form.$private.events.on('change', function(fieldName, oldValue, newValue) {
                  if (fieldName == field.name) {} else if (fieldName == field.dependentField) {
                      field.dependentValue = newValue;
                      refreshChoiceList();
                  } else if (typeof field.variable_name !== 'undefined' &amp;&amp; field.reference_qual &amp;&amp; isRefQualElement(fieldName)) {
                      refreshReferenceChoices();
                  }
              });
  
              function isRefQualElement(fieldName) {
                  var refQualElements = [];
                  if (field.attributes &amp;&amp; field.attributes.indexOf('ref_qual_elements') &gt; -1) {
                      var attributes = spUtil.parseAttributes(field.attributes);
                      refQualElements = attributes['ref_qual_elements'].split(',');
                  }
                  return field.reference_qual.indexOf(fieldName) != -1 || refQualElements.indexOf(fieldName) != -1;
              }
  
              function refreshChoiceList() {
                  var params = {};
                  params.table = g_form.getTableName();
                  params.field = field.name;
                  params.sysparm_dependent_value = field.dependentValue;
                  var url = urlTools.getURL('choice_list_data', params);
                  return $http.get(url).success(function(data) {
                      field.choices = [];
                      angular.forEach(data.items, function(item) {
                          field.choices.push(item);
                      });
                      selectValueOrNone();
                  });
              }
  
              function selectValueOrNone() {
                  var hasSelectedValue = false;
                  angular.forEach(field.choices, function(c) {
                      if (field.value == c.value)
                          hasSelectedValue = true;
                  });
                  if (!hasSelectedValue &amp;&amp; field.choices.length &gt; 0) {
                      g_form.setValue(field.name, field.choices[0].value, field.choices[0].label);
                  }
             }
  
              function refreshReferenceChoices() {
                  var params = [];
                  params['qualifier'] = field.reference_qual;
                  params['table'] = field.lookup_table;
                  params['sysparm_include_variables'] = true;
                  params['variable_ids'] = field.sys_id;
                  var getFieldSequence = g_form.$private.options('getFieldSequence');
                  if (getFieldSequence) {
                      params['variable_sequence1'] = getFieldSequence();
                  }
                  var itemSysId = g_form.$private.options('itemSysId');
                  params['sysparm_id'] = itemSysId;
                  var getFieldParams = g_form.$private.options('getFieldParams');
                  if (getFieldParams) {
                      angular.extend(params, getFieldParams());
                  }
                  var url = urlTools.getURL('sp_ref_list_data', params);
                  return $http.get(url).success(function(data) {
                      field.choices = [];
                      angular.forEach(data.items, function(item) {
                          item.label = item.$$displayValue;
                          item.value = item.sys_id;
                          field.choices.push(item);
                      });
                      selectValueOrNone();
                  });
              }
              var pcTimeout;
              g_form.$private.events.on('propertyChange', function(type, fieldName, propertyName) {
                  if (fieldName != field.name)
                      return;
                  if (propertyName == "optionStack") {
                      $timeout.cancel(pcTimeout);
                      pcTimeout = $timeout(function() {
                          field.choices = applyOptionStack(fieldOptions, field.optionStack);
                          selectValueOrNone();
                      }, 35);
                  }
              });
              setDefaultOptions();
              if (field.choices) {
                  setChoiceOptions(field.choices);
              }
              selectValueOrNone();
  
              function setDefaultOptions() {
                  setChoiceOptions([{
                      value: scope.field.value,
                      label: scope.field.displayValue || scope.field.placeholder
                  }]);
              }
  
              function setChoiceOptions(options) {
                  if (options) {
                      options.forEach(function(option) {
                          option.value = String(option.value);
                      });
                  }
                  fieldOptions = options;
                  scope.options = applyOptionStack(options, scope.field.optionStack);
              }
  
              function applyOptionStack(options, optionStack) {
                  if (!optionStack || optionStack.length == 0) {
                      return options;
                  }
                  var newOptions = angular.copy(options);
                  if (!newOptions) {
                      newOptions = [];
                  }
                  optionStack.forEach(function(item) {
                      switch (item.operation) {
                          case 'add':
                              for (var o in newOptions) {
                                  if (newOptions[o].label == item.label)
                                      return;
                              }
                              var newOption = {
                                  label: item.label,
                                  value: item.value
                              };
                              if (typeof item.index === 'undefined') {
                                  newOptions.push(newOption);
                              } else {
                                  newOptions.splice(item.index, 0, newOption);
                              }
                              break;
                          case 'remove':
                              var itemValue = String(item.value);
                              for (var i = 0, iM = newOptions.length; i &lt; iM; i++) {
                                  var optionValue = String(newOptions[i].value);
                                  if (optionValue !== itemValue) {
                                      continue;
                                  }
                                  newOptions.splice(i, 1);
                                  break;
                              }
                              break;
                          case 'clear':
                              newOptions = [];
                              break;
                          default:
                      }
                  });
                  return newOptions;
              }       
          }
      };
  }
  </code></pre>