Angularjs Angular js使用JSON填充下拉列表

Angularjs Angular js使用JSON填充下拉列表,angularjs,Angularjs,我对angular js是新手,正在学习。今天我得到了一个使用angular用JSON填充下拉列表的代码。这是代码 <select ng-model="selectedTestAccount" ng-options="item.Id as item.Name for item in testAccounts"> <option value="">Select Account</option> </select> 这我不清楚。ng opti

我对angular js是新手,正在学习。今天我得到了一个使用angular用JSON填充下拉列表的代码。这是代码

<select ng-model="selectedTestAccount" ng-options="item.Id as item.Name for item in testAccounts">
    <option value="">Select Account</option>
</select>
这我不清楚。ng options=item.Id as item.Name for item in testAccounts为什么在两个字段名之间使用as。若我需要指定3个字段,那个么对于testAccounts中的项,代码看起来像ng options=item.Id as item.Name as item.Desc

请帮助我了解ng选项

还请告诉我为什么需要此SelectedStatAccount

这样我就填充了下拉列表,但第一次在下拉列表中添加空行时……我不明白为什么会发生这种情况

第二个问题是,当我从下拉列表中选择国家时,国家id不显示。这是我的密码。请看一看,给我引路

<div ng-controller="DemoCtrl" ng-app="main">
 <p>populate select options with ajax</p>

<select name="cboCountry" ng-model="selectedCountry">
<option ng:repeat="facility in chooseCountries" value="{{facility.id}}">{{facility.name}}</option>
</select>  
<span>Selected country id is {{selectedCountry.countryId}}</span>   

</div>

var app = angular.module('main', []);
app.controller('DemoCtrl', function ($scope) {

    $scope.chooseCountries=[
        {countryId : 1, name : "France - Mainland", desc: "some description" },
        {countryId : 2, name : "Gibraltar", desc: "some description"},
        {countryId : 3, name : "Malta", desc: "some description"}
    ];

    $scope.selectedCountry = angular.copy($scope.chooseCountries[0]);
});

感谢

这可能是因为id和name是response to get请求范围的testAccounts中的项数组中的字段。试着在控制器逻辑中调试设置断点,你们会明白的

您还可以使用以下代码

<select ng-model="selectedTestAccount">
 <option value="">Select Account</option> 
<option ng-repeat="item in testAccounts" value="item.Id">{{item.Name}}    
</option></select>
你想使用更多的领域,你可以使用像

<select ng-model="selectedTestAccount">
 <option value="">Select Account</option> 
<option ng-repeat="item in testAccounts" value="item.Id">
  {{item.Id}}-{{item.Name}}
</option></select>
此SelectedStatAccount的工作方式与javascript中的Id相同,有助于获取所选值

var-app=angular.module'main',[]; app.controller'DemoCtrl',函数$scope{ $scope.chooseCountries=[ {countryId:1,名称:法国-大陆,desc:some description}, {countryId:2,name:Gibraltar,desc:some description}, {countryId:3,姓名:马耳他,描述:一些描述} ]; $scope.selectedCountry=$scope.chooseCountries[0].countryId; }; 选择帐户 试试这个

<div ng-controller="DemoCtrl" ng-app="main">
  <select ng-model="selectedCountry">
<option value="">Select Account</option>
      <option ng-repeat="x in chooseCountries" value="{{x.countryId}}"  >{{x.name}}</option>
</select>  
</div>

请在底部发布您的json数据,我再次发布json数据和代码。请看一看。不理解此行“item.countryId为item.name for item in chooseCountries”。为什么我们需要使用as关键字指定字段名,因为我们在sql中使用as关键字指定别名。指南plzzIt类似于C中的DataTextfield和DataValueField。DataTextfield是它将在dropdownlist中显示的字段,DataValueField是您将在代码中选择的值,我们可能需要实现一些业务逻辑。在这种情况下,item.name是您在下拉列表中看到的值,item.id是用于操作业务逻辑的datatext字段。如果我的json值是这样的[{FRIDAY:[{id:1,name:xyz}]}],并且我想选择名称,该怎么办?到星期五,我试过你的方法;但是我的数据是空的?但是你的数据结构和OP的数据结构不同。您到底想要什么?我测试了您的代码并以这种方式设置selectedTestAccount,但$scope不起作用。selectedTestAccount=$scope.chooseCountries[0]。countryId;下拉式渲染时未选择任何国家/地区数据……代码中缺少的内容。把JSFIDLE给我。谢谢。现在可以用了。当ng value=x.countryId以这种方式设置所选国家时,请告诉我$scope.selectedCountry=$scope.chooseCountries[1]。然后告诉我它是如何工作的,因为我引用的是名称,而ng value引用的是国家id。请帮助我理解。谢谢,我将此方式更改为选择国家,但此行不起作用“$scope.selectedCountry=$scope.chooseCountries[1].countryId.toString;”出现了什么错误?我尝试使用这两个更改ng value=x.countryId和$scope.selectedCountry=$scope.chooseCountries[1]。名称但我得到了空行。请正确执行更改。它是怎么工作的?我不明白你想说什么?请帮我确定错误是什么?
<div ng-controller="DemoCtrl" ng-app="main">
  <select ng-model="selectedCountry">
<option value="">Select Account</option>
      <option ng-repeat="x in chooseCountries" value="{{x.countryId}}"  >{{x.name}}</option>
</select>  
</div>
var app = angular.module('main', []);
app.controller('DemoCtrl', function ($scope) {
$scope.chooseCountries=[
    {countryId : 1, name : "France - Mainland", desc: "some description" },
    {countryId : 2, name : "Gibraltar", desc: "some description"},
    {countryId : 3, name : "Malta", desc: "some description"}
];   
 $scope.selectedCountry =  $scope.chooseCountries[1].countryId.toString();   
});