gpt4 book ai didi

javascript - 为什么这个选择框不显示 'Select One' 选项?

转载 作者:行者123 更新时间:2023-11-30 15:07:47 24 4
gpt4 key购买 nike

我有一个使用 ng-option 动态填充的选择框。我希望它默认显示“选择一个”。我使用以下 html 希望实现这一目标。

<select id="productType" name="productType" class="col-lg-12 col-xs-12 form-control"
ng-model="context.product"
ng-options="product.value as product.name for product in data.products"
ng-change="loadAccessMethods()" required>
<option value="" disabled>Select one</option>
</select>

但这是我在运行应用程序时看到的。

enter image description here

更新:我正在使用 Angular 1.6.5

更新:Vivz 在他的回答中添加了一个使用 Angular 1.4.x 的工作代码片段。但它不适用于 Angular 1.6.x。任何人都可以指出为什么这在 Angular 1.6.x 中被破坏了吗?

angular.module("App",[])
.controller("AppCtrl",function($scope){
$scope.context = {
product: '',
accessMethod: '',
workOrderNum: 0
};

$scope.data = {
products: [
{ name: 'ABC', value: '0', type: 't1' },
{ name: 'XYZ', value: '1', workflowType: 't2' }
]
};

});
<!DOCTYPE html>
<html ng-app="App">
<head>
<meta charset="utf-8" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- <script data-require="angular.js@1.4.9" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script> -->
<script src=" https://opensource.keycdn.com/angularjs/1.6.5/angular.min.js "></script>
<script src="script.js"></script>
</head>

<body>
<div ng-controller="AppCtrl">
<select id="productType" name="productType" class="col-lg-12 col-xs-12 form-control"
ng-model="context.product"
ng-options="product.value as product.name for product in data.products"
ng-change="loadAccessMethods()" required>
<option value="" disabled>Select one</option>
</select>
</div>
</body>

</html>

最佳答案

您可以尝试这样的不同方法:

var app = angular.module("App", []);
app.controller("AppCtrl", function($scope) {
$scope.context = {
product: '',
accessMethod: '',
workOrderNum: 0
};
$scope.data = {
products: [{
name: 'ABC',
value: '0',
type: 't1'
},
{
name: 'XYZ',
value: '1',
workflowType: 't2'
}
]
};

$scope.context.product1 = '';

});
<!DOCTYPE html>
<html ng-app="App">

<head>
<meta charset="utf-8" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<!-- <script data-require="angular.js@1.4.9" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script> -->
<script src=" https://opensource.keycdn.com/angularjs/1.6.5/angular.min.js "></script>
<script src="script.js"></script>
</head>

<body>
<div ng-controller="AppCtrl">
<h1> First Apporach</h1>
<select id="productType" name="productType" class="col-lg-12 col-xs-12 form-control" ng-model="context.product" ng-change="loadAccessMethods()" required>
<option value="" disabled>Select one</option>
<option ng-value="product.value" ng-repeat=" product in data.products">{{product.name}}</option>
</select>


<h2>Second approaach</h2>
<span ng-init="data.products.unshift({ name: 'Select one', value: ''});"></span>
<select id="productType" name="productType" class="col-lg-12 col-xs-12 form-control"
ng-model="context.product1"
ng-change="loadAccessMethods()" ng-options="product.value as product.name for product in data.products" required>
</select>
</div>


</body>

</html>

关于javascript - 为什么这个选择框不显示 'Select One' 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com