[英]Weird behavior in select/option tag in angular
這是我的代碼片段:
<select class="form-control" name="" id="" ng-model="offerId">
<option value="">Choose</option>
<option ng-repeat="offer in offers " value="{{ offer.id }}"
ng-selected="offerId == offer.id">
{{offer.supplier.name}}
({{offer.request.name}})
</option>
</select>
它可以按需工作。 問題或怪異的行為是,如果我從其他組件/觸發器更改了ngModel(offerId),則列表中添加了新的空白選項; 它的行為與默認的“選擇”選項相同,並且當從選擇/選項中選擇了任何選項(包括空白選項)時,空格選項消失了,其他功能也一樣,只是空白空間很奇怪。
我還使用了ngOptions,但是功能發生了變化,因此我要堅持使用此實現。
這是功能嗎?
一些代碼片段:
$scope.clearAttachedFiles = function () {
$scope.attachedFiles = {uploads: null, links: null};
};
$scope.$watch('offerId', function (a, b) {
if ($scope.offerId) {
offerId = $scope.offerId;
}
if ((typeof a == 'undefined' || typeof a == 'null') && (typeof b == 'undefined' || typeof b == 'null')) {
return;
}
if (a === '') {
$scope.level = 'EVENT';
$scope.privateMessage = true;
$scope.offer = null;
}
else {
$scope.level = 'OFFER';
$scope.privateMessage = false;
angular.forEach($scope.offers, function (offer) {
if (offer.id == a) {
$scope.offer = offer;
}
});
}
$scope.getPaginatedActivities();
$scope.resetActivitiesList();
$scope.currentPage = 1;
});
您使用的方式有誤,ng-options存在是有原因的。
當您使用ng-repeat時,您為數組中的每個選項添加了一個觀察程序,因此性能可能真的很差。 另外,執行value = {{}}也會有問題,因為這不是正確的處理方法,具體取決於編譯時間和其他因素,select可能會將{{}}用作值而不是所求值的表達式。 可能是選擇空白的原因。
使用ngOptions,非常靈活且易於使用。 這里是文檔: https : //docs.angularjs.org/api/ng/directive/ngOptions
提出任何與您相關的問題!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.