Angularjs 角度国家/地区选择模块未选择默认值
我正在使用模块。 我无法将默认选定值设置为下拉列表Angularjs 角度国家/地区选择模块未选择默认值,angularjs,jquery-select2,Angularjs,Jquery Select2,我正在使用模块。 我无法将默认选定值设置为下拉列表 <input country-select data-ng-model="userCtrl.country" class="signup-country" placeholder="Country*" ng-required="true" name="country" id="signUpCountry" value="{{userCtrl.country}}"> }])) 但这也不会选择默认值。 非常感谢您的帮助。我可以通过添加
<input country-select data-ng-model="userCtrl.country" class="signup-country" placeholder="Country*" ng-required="true" name="country" id="signUpCountry" value="{{userCtrl.country}}">
}]))
但这也不会选择默认值。
非常感谢您的帮助。我可以通过添加$timeout来选择默认值 以下是更新的模块代码:
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);
}
};
}]);
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);
}
};
}]);