angular-country-select 模块没有 selecting 默认值

angular-country-select module not selecting default value

我正在使用 angular-country-select 模块。 我无法将默认 selected 值设置为下拉列表。

<input country-select data-ng-model="userCtrl.country" class="signup-country" placeholder="Country*" ng-required="true" name="country" id="signUpCountry" value="{{userCtrl.country}}">

我尝试修改模块代码以将 val 传递给 select2(因为该模块使用 select2)。下面是修改后的代码:

angular.module('angular-country-select', [])
.directive('countrySelect', [function() {
return {
  restrict: 'A',
  require:'ngModel',
  link: function(scope, elem, attrs, ngModelCtrl) { console.log("elem");
    var data = ["id":"AX","text":"Åland Islands"},{"id":"AL","text":"Albania"}, ....]; //This array has values
    var defaultValue = attrs.value || '';
      ngModelCtrl.$setViewValue(defaultValue);
    var select2Inst = elem.select2({
      data: data,
      val: defaultValue
    });
  }
};

}]);

但这也不是select默认值。 感谢任何帮助。

我可以通过添加 $timeout select 默认值 以下是更新的模块代码:

angular.module('angular-country-select', [])
.directive('countrySelect', ['$timeout', function($timeout) {
  return {
  restrict: 'A',
  require:'ngModel',
  link: function(scope, elem, attrs, ngModelCtrl) { console.log(attrs, attrs.placeholder);
    var data = [{"id":"AF","text":"Afghanistan"},{"id":"AX","text":"Åland Islands"},...];
    var select2Inst = elem.select2({
      data: data
    });
    $timeout(function() {
      if (attrs.value != '') {
        ngModelCtrl.$setViewValue(attrs.value);
        scope.$apply(select2Inst.select2('val', attrs.value));
      }
    }, 2000);
  }
};
}]);