Javascript 对响应对象使用ng选项

Javascript 对响应对象使用ng选项,javascript,angularjs,Javascript,Angularjs,关于如何使用ng选项,我已经找到了很多答案,但还没有找到解决方案或正确的方法 我的代码很简单。我想遍历收到的响应对象,并用name属性填充“select”标记的“options” index.js app.controller("promo", function($scope, $http) { $scope.behaviors = {}; $scope.init = function() { return $http.get('/behaviors').then(function(

关于如何使用ng选项,我已经找到了很多答案,但还没有找到解决方案或正确的方法

我的代码很简单。我想遍历收到的响应对象,并用name属性填充“select”标记的“options”

index.js

app.controller("promo", function($scope, $http) {

$scope.behaviors = {};

$scope.init = function() {
    return $http.get('/behaviors').then(function(response) {
         $scope.behaviors = response.data;
         console.log(typeof($scope.behaviors))
    })
}
我的回答是这样的

Object {bangalore.yml: Object, ean.yml: Object, chennai.yml: Object}
我想我可以用ng repeat这样做

<select ng-model="behavior" ng-repeat="behavior in behaviors">
<option>{{behavior.name}}</option>

{{behavior.name}

我是一个初学者,我需要知道该做什么以及ng选项是如何工作的

如您所说,使用ng选项:

<select ng-model="behavior" ng-options="value as key for (key, value) in behaviors">

这将所选值绑定到
行为
,显示选项将是对象的键。 还请注意,您不需要
选项
元素。

您应该使用

如果您想显示名称并绑定整个对象,请使用下面的

<select ng-model="behavior" ng-options="behavior.name for behavior in behaviors"></select>
<select ng-model="behavior" ng-options="behavior.is as behavior.name for behavior in behaviors">
</select>

如果要显示该对象的名称和绑定id,请使用下面的

<select ng-model="behavior" ng-options="behavior.name for behavior in behaviors"></select>
<select ng-model="behavior" ng-options="behavior.is as behavior.name for behavior in behaviors">
</select>

使用:-

 <select ng-model="behavior" ng-options=" value.name for (key, value) in behaviors">

      </select>


这种方法很有效。现在它向我展示了“班加罗尔yml、钦奈yml和ean.yml”。但是,我需要显示每个对象中的name属性。你知道该怎么做吗?你的回答“behavior.name”中的name字段在哪里?很抱歉,我无法得到您的正确回复。您能提供更多信息吗?ng repeat将应用于not on或使用ng选项每个对象,即“bangalore.yml”都有属性html、位置和名称。Ping检查我的答案,希望它能帮助您。谢谢!它起作用了。您能解释一下它是如何工作的吗?您有一个分配给$scope.behaviors的对象响应。因此,通过使用ng选项,您可以提取每个元素的键和值。键是['bangalore.yml','ean.yml','chennai.yml'],值也是由名称、位置和,因此,在ng选项中,我们提取每个值并为每个选项指定value.name。