我将数据存储在product.taxonmyF1和product.taxonomyF2的数组中。我需要根据某些标准在多选和单选之间切换。如果我有一个选择它不会预先选择选择框,因为它不期望一个数组。实现这个的最佳方法是什么?
以下是我的代码的外观:
<!-- Coding/marking -->
<div ng-if="product.taxonomy == CONSTANTS.CODING_MARKING" layout="row">
<md-input-container class="md-default-theme full-width-select">
<md-select ng-model="product.taxonomyF1" placeholder="Select All That Apply" multiple="true">
<md-option value="[[id]]" ng-repeat="(id, name) in taxonomies.subTermOptions[CONSTANTS.CODING_MARKING][CONSTANTS.CODING_MARKING_F1]" id="category_[[id]]">[[name]]</md-option>
</md-select>
</md-input-container>
<md-input-container class="md-default-theme full-width-select">
<md-select ng-model="product.taxonomyF2" placeholder="Select One" multiple="true">
<md-option value="[[id]]" ng-repeat="(id, name) in taxonomies.subTermOptions[CONSTANTS.CODING_MARKING][CONSTANTS.CODING_MARKING_F2]" id="category_[[id]]">[[name]]</md-option>
</md-select>
</md-input-container>
</div>
<!-- Case erect/seal -->
<div ng-if="product.taxonomy == CONSTANTS.CASE_ERECT_SEAL" layout="row">
<md-input-container class="md-default-theme full-width-select">
<md-select ng-model="product.taxonomyF1" placeholder="Select One" >
<md-option value="[[id]]" ng-repeat="(id, name) in taxonomies.subTermOptions[CONSTANTS.CASE_ERECT_SEAL][CONSTANTS.CASE_ERECT_SEAL_F1]" id="category_[[id]]">[[name]]</md-option>
</md-select>
</md-input-container>
<md-input-container class="md-default-theme full-width-select" ng-hide="product.isUsedInPrint">
<md-select ng-model="product.taxonomyF2" placeholder="Select All That Apply" >
<md-option value="[[id]]" ng-repeat="(id, name) in taxonomies.subTermOptions[CONSTANTS.CASE_ERECT_SEAL][CONSTANTS.CASE_ERECT_SEAL_F2]" id="category_[[id]]">[[name]]</md-option>
</md-select>
</md-input-container>
</div>
如果是多选,我添加倍数=真。
即使我的product.taxonomyF1看起来像这样,我希望我的单一选择预选:
taxonomyF1:{
0: "19"
}
但它只有在这样的情况下才有效:
taxonomyF1: 19
答案 0 :(得分:0)
在我的场景中,我修复了它:
/**
* Convert single select values from array to integer.
*
* @param product
*/
$scope.convertSingleSelects = function(p) {
angular.forEach(p, function (object) {
// If the taxonomy is one of these the taxonomyF1 field will be a single
// select.
var taxonomyForF1SingleSelects = [
CONSTANTS.BAG_SEALING_CLOSING,
CONSTANTS.CARTONING,
CONSTANTS.CASE_ERECT_SEAL,
CONSTANTS.CASE_TRAY_PACKING,
CONSTANTS.CONTROLS_AND_MACHINE_COMPONENTS,
CONSTANTS.CONVEYING_AND_ACCUMULATI0N,
CONSTANTS.FILLING_CAPPING_DRY,
CONSTANTS.FILLING_CAPPING_LIQUID_AND_VISCOUS,
CONSTANTS.FORM_FILL_SEAL_HORIZONTAL,
CONSTANTS.FORM_FILL_SEAL_VERTICAL,
CONSTANTS.INSPECT_DETECT_CHECKWEIGH_VISION,
CONSTANTS.LABELING,
CONSTANTS.MULTIPACKING_SHRINK_BUNDLING,
CONSTANTS.PALLETIZING_DEPALLETIZING,
CONSTANTS.TAMPER_EVIDENCE_MACHINERY,
CONSTANTS.ROBOTICS,
CONSTANTS.WEIGHING_COMBINATION_SCALES
];
// If the taxonomy is one of these the taxonomyF2 field will be a single
// select.
var taxonomyForF2SingleSelects = [
CONSTANTS.CODING_MARKING,
CONSTANTS.FILLING_CAPPING_LIQUID_AND_VISCOUS,
];
// Setting the first value of the first index as the value.
if (object.taxonomy != null || object.taxonomy != '') {
if (taxonomyForF1SingleSelects.indexOf(object.taxonomy) != -1) {
if (object.taxonomyF1[0] != undefined) {
object.taxonomyF1 = object.taxonomyF1[0];
}
if (object.taxonomyF2[0] != undefined) {
object.taxonomyF2 = object.taxonomyF2[0];
}
}
}
});
};
// Single selects are in an array so convert them to integers instead.
$scope.convertSingleSelects($scope.webProducts);
angular.forEach($scope.printProducts.taxonomies, function(object) {
$scope.convertSingleSelects(object.products);
});