[英]ng-options with pre-selected value using ng-model not working
I know there are lots of similar questions all around, but i have no clues why this is not working for me? 我知道到处都有很多类似的问题,但是我不知道为什么这对我不起作用? I want size '4x4' as preselected in the dropdown. 我想要下拉菜单中预先选择的尺寸“ 4x4”。 '4x4' is now static but finally i will replace '4x4' with a variable that will provide dynamic sizes. “ 4x4”现在是静态的,但最终我将用提供动态大小的变量替换“ 4x4”。
Here is my Controller code: 这是我的控制器代码:
$scope.sizes = [{
size: '4 x 4',
price: 1.00
},
{
size: '4 x 6',
price: 5.00
},
{
size: '4 x 8',
price: 7.00
},
{
size: '5 x 6',
price: 9.00
},
{
size: '5 x 8',
price: 10.00
},
{
size: '6 x 8',
price: 15.00
}]
$scope.selected = {
"size": '4 x 4'
}
And here is view code: 这是查看代码:
<select class="form-control" ng-model="selected.size" ng-options="item.size for item in sizes">
</select>
try this 尝试这个
$scope.selected = $scope.sizes[0];
<select class="form-control" ng-model="selected" ng-options="item.size for item in sizes">
Demo 演示版
angular.module("App", []) .controller("mainCtrl", function($scope){ $scope.sizes = [{ size: '4 x 4', price: 1.00 }, { size: '4 x 6', price: 5.00 }, { size: '4 x 8', price: 7.00 }, { size: '5 x 6', price: 9.00 }, { size: '5 x 8', price: 10.00 }, { size: '6 x 8', price: 15.00 }] $scope.selected = $scope.sizes[0]; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="App" ng-controller="mainCtrl as keyVar" class="container"> <select class="form-control" ng-model="selected" ng-options="item.size for item in sizes"> </select> </div>
I would change the template to 我将模板更改为
<select class="form-control" ng-model="selected" ng-options="item.size for item in sizes">
</select>
and the controller to 和控制器
$scope.selected = $scope.sizes[0];
Call first object in ng-init
ng-init="selected.size = sizes[0];"
在ng-init中调用第一个对象ng-init="selected.size = sizes[0];"
and should initializeselected
object like$scope.selected={}
并应初始化selected
对象,例如$scope.selected={}
angular.module("App", []) .controller("mainCtrl", function($scope){ $scope.sizes = [{ size: '4 x 4', price: 1.00 }, { size: '4 x 6', price: 5.00 }, { size: '4 x 8', price: 7.00 }, { size: '5 x 6', price: 9.00 }, { size: '5 x 8', price: 10.00 }, { size: '6 x 8', price: 15.00 }] $scope.selected={}; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="App" ng-controller="mainCtrl as keyVar" class="container"> <select class="form-control" ng-model="selected.size" ng-init="selected.size = sizes[0];" ng-options="item.size for item in sizes"> </select> </div>
'4x4' is now static but finally i will replace '4x4' with a variable that will provide dynamic sizes. “ 4x4”现在是静态的,但最终我将用提供动态大小的变量替换“ 4x4”。
You can create a new scope object (as YourDynamicObject
) and assign the dynamic value. 您可以创建一个新的作用域对象(作为YourDynamicObject
)并分配动态值。 it will be affect in ng-init 会影响ng-init
Code like 像这样的代码
ng-init="selected.size = YourDynamicObject" // html
$scope.YourDynamicObject=$scope.size[dynamicindex];//controller
try some thing like this: 尝试这样的事情:
<select ng-init="selected= sizes[0]"
ng-model="selected"
ng-options="item.size for item in sizes">
</select>
angular.module("App", []) .controller("mainCtrl", function($scope){ $scope.sizes = [{ size: '4 x 4', price: 1.00 }, { size: '4 x 6', price: 5.00 }, { size: '4 x 8', price: 7.00 }, { size: '5 x 6', price: 9.00 }, { size: '5 x 8', price: 10.00 }, { size: '6 x 8', price: 15.00 }] $scope.selected = $scope.sizes[0]; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="App" ng-controller="mainCtrl as keyVar" class="container"> <select class="form-control" ng-model="selected" ng-options="item.size for item in sizes"> </select> <p>{{selected}}</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.