0

在第一个片段中,当使用 ng-value 指令时,ng-value= 到 json,表现得像一个对象,我可以得到 json 格式并像一个对象一样使用它。但是,如果我使用 value 属性,我就不能像对象一样使用它。为什么在第二个片段中我无法显示 {{selectedName.a}} ?谢谢

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<select ng-model="selectedName">
<option ng-repeat="item in names" ng-value="{{item}}">{{item.a }}</option>
</select>
<p>{{selectedName}}</p>   <!-- its a json stracture but -->
<p>{{selectedName.a}}</p>  <!-- behave like an object?!?!? -->
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = [
    {"a":"Emil1", "b":"Tobias1", "c":"Linus1"},
    {"a":"Emil2", "b":"Tobias2", "c":"Linus2"},
    {"a":"Emil3", "b":"Tobias3", "c":"Linus3"}
    ];
});
</script>

<p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</body>
</html>

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<select ng-model="selectedName">
<option ng-repeat="item in names" value="{{item}}">{{item.a }}</option>
</select>
<p>{{selectedName}}</p> 
<p>{{selectedName.a}}</p>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = [
    {"a":"Emil1", "b":"Tobias1", "c":"Linus1"},
    {"a":"Emil2", "b":"Tobias2", "c":"Linus2"},
    {"a":"Emil3", "b":"Tobias3", "c":"Linus3"}
    ];
});
</script>

<p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</body>
</html>

4

0 回答 0