gpt4 book ai didi

javascript - angularJS - nginit,如何获得范围

转载 作者:行者123 更新时间:2023-11-30 12:00:54 25 4
gpt4 key购买 nike

https://jsfiddle.net/jzhang172/7c2zgkf0/1/

angularJS如何设置select的初始值?

var app = angular.module('myApp', []);

app.controller('carCtrl',function($scope){
$scope.cars={
car01 : {brand : "Ford", model : "Mustang", color : "red"},
car02 : {brand : "Fiat", model : "500", color : "white"},
car03 : {brand : "Volvo", model : "XC90", color : "black"}

}

});
input.ng-invalid{
background:red;
transition:.4s;
}
input.ng-valid{
background:#A3CBFF;
transition:.4s;
}
input.ng-pending{
border:1px solid red;
}
input{
font-size: 30px;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>


<div ng-app="myApp" ng-controller="carCtrl" ng-init="selectCar='$scope.cars.car01'">
<p>
Please Select a Car
</p>
<select ng-model="selectCar" ng-options="x for (x,y) in cars" >

</select>
<span>Color: {{selectCar.color}}</span>
<span>Model: {{selectCar.model}}</span>
<span>Brand: {{selectCar.brand}}</span>
</div>

最佳答案

你可以使用ng-model

var app = angular.module('myApp', []);

app.controller('carCtrl',function($scope){

$scope.cars={
car01 : {brand : "Ford", model : "Mustang", color : "red"},
car02 : {brand : "Fiat", model : "500", color : "white"},
car03 : {brand : "Volvo", model : "XC90", color : "black"}

}
$scope.initVal = $scope.cars['car01'];
});

<select ng-model="initVal" ng-options="x for (x,y) in cars" >

关于javascript - angularJS - nginit,如何获得范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638970/

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