简体   繁体   English

angularJS - nginit,如何获得范围

[英]angularJS - nginit, how to get scope

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

How do I set the initial value of select with angularJS? 如何使用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> 

you can use ng-model 你可以使用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" >

You set the initial value by setting the value of $scope.selectCar . 您可以通过设置$scope.selectCar值来设置初始值。 That's where Angular will look into. 这就是Angular将要研究的地方。

You can also add something like 你也可以添加类似的东西

<option disabled selected>-- Select --</option>

If you just want to show a selection message. 如果您只想显示选择消息。

.

Now, if you set $scope.selectCar to some item in the list, like: 现在,如果将$scope.selectCar设置$scope.selectCar中的某个项目,例如:

$scope.selectCar = $scope.cars['car01'];

Angular will understand it's the same object and select it. Angular会理解它是同一个对象并选择它。

But if it's not from the same parent object / list, even if it has the same properties, AngularJS will NOT know it's the same object (because object comparison in JS is by reference not by value) and will not select it. 但是如果它不是来自同一个父对象/列表,即使它具有相同的属性,AngularJS也不会知道它是同一个对象(因为JS中的对象比较是通过引用而不是值)并且不会选择它。

In this case you can add a track by expression. 在这种情况下,您可以track by表达式添加track by If you have a unique "id" property in each car object (doesn't have to be exactly called id ), you can append this to your ng-options expression: ng-options="blah blah ... in cars track by x.id" . 如果你在每个汽车对象中都有一个唯一的“id”属性(不必完全称为id ),你可以将它附加到ng-options表达式: ng-options="blah blah ... in cars track by x.id"

I have a very detailed blog post about all the different situations you might have with this: 我有一篇非常详细的博客文章,介绍了您可能遇到的所有不同情况:

How to set the initial selected value of a select element using Angular.JS ng-options & track by 如何使用Angular.JS ng-options和track by设置选择元素的初始选定值

If you have any problems using track-by using Angular 1.4+, or just want to know which field to use so that you are using it correctly, see this post for help as well: 如果您在使用Angular 1.4+ track-by遇到任何问题,或者只是想知道要使用哪个字段以便正确使用它,请参阅此帖子以获取帮助:

Using track by correctly with Angular 1.4 select ng-options – Why can't I select this option? 正确使用Angular 1.4选择ng-options - 为什么我不能选择此选项?

Let me know how it goes. 让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM