0

如何使用ng-option我可以從選擇值中獲得一個對象 但是,如果我使用ng-repeat選項指令,我可以得到只是字符串。 我試圖從串NG-重複轉換與angular.fromJson()反對,但我失敗了: 我的代碼是:如何從ng-repeat使用angular.fromJson()

<!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"> 

<p>Select a car:</p> 

<select ng-model="selectedCar"> 
<option ng-repeat="(x, y) in cars" value="{{y}}">{{y.brand}}</option> 
</select> 

<input type="text" value="{{selectedCar}}"> 
<h1>You selected: {{selectedCarObj.brand}}</h1> 
<h2>Model: {{selectedCarObj.model}}</h2> 
<h3>Color: {{selectedCarObj.color}}</h3> 

<h1>You selected: {{selectedCar}}</h1> 
<h2>Model: {{selectedCar}}</h2> 
<h3>Color: {{selectedCar}}</h3> 

<p>The visible text inside the dropdown list can also be a property of the value object.</p> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', 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.selectedCarObj = angular.fromJson($scope.selectedCar); 
}); 
</script> 

</body> 
</html> 

如果我改變該行:

$scope.selectedCarObj = angular.fromJson($scope.selectedCar); 

要:

$scope.selectedCarObj = angular.fromJson({brand : "Ford", model : "Mustang", color : "red"}); 

它工作!

爲什麼我不能獲得{{selectedCarObj.brand}}值?

上的選擇LIS選項值:

<option ng-repeat="(x, y) in cars" value="{"brand":";Fiat";,";model":"500","color":"white"}" class="ng-binding ng-scope">Fiat</option><!-- end ngRepeat: (x, y) in cars --> 

有人可以幫助我理解爲什麼它不工作?

也許html caharacters?!?

非常感謝。

回答

1

你缺少NG-變化:

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', 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.GetObject = function(){ 
 
    $scope.selectedCarObj = angular.fromJson($scope.selectedCar); 
 
    
 
} 
 
});
<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"> 
 

 
<p>Select a car:</p> 
 

 
<select ng-model="selectedCar" ng-change="GetObject()"> 
 
<option ng-repeat="(x, y) in cars" value="{{y}}">{{y.brand}}</option> 
 
</select> 
 

 
<input type="text" value="{{selectedCar}}"> 
 
<h3>Json: {{selectedCar}}</h3> 
 
object is : 
 
<h3>brand: {{selectedCarObj.brand}}</h3> 
 
<h3>model: {{selectedCarObj.model}}</h3> 
 
<h3>color: {{selectedCarObj.color}}</h3> 
 

 
<p>The visible text inside the dropdown list can also be a property of the value object.</p> 
 

 
</div> 
 
</body> 
 
</html>

+0

太好了!非常感謝你!我錯過了ngChange指令。再次感謝;-) – user75486

0

另一種替代方案是創建一個自定義過濾器的字符串轉換爲對象,並返回相關的屬性

app.filter('jsonFilt',function(){ 
    return function(item,name){ 
    if(item){ 
     item = JSON.parse(item); 
     return item[name]; 
    } 
    } 
}); 

在這樣的HTML中調用過濾器

<input type="text" value="{{selectedCar}}"> 
<h1>You selected: {{selectedCar | jsonFilt : 'brand'}}</h1> 
<h2>Model: {{selectedCar | jsonFilt : 'model'}}</h2> 
<h3>Color: {{selectedCar | jsonFilt : 'color'}}</h3> 

演示

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', 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.selectedCarObj = angular.fromJson($scope.selectedCar); 
 
}); 
 
app.filter('jsonFilt',function(){ 
 
    return function(item,name){ 
 
    if(item){ 
 
     item = JSON.parse(item); 
 
     return item[name]; 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 

 
<p>Select a car:</p> 
 

 
<select ng-model="selectedCar"> 
 
<option ng-repeat="(x, y) in cars" value="{{y}}">{{y.brand}}</option> 
 
</select> 
 

 
<input type="text" value="{{selectedCar}}"> 
 
<h1>You selected: {{selectedCar | jsonFilt : 'brand'}}</h1> 
 
<h2>Model: {{selectedCar | jsonFilt : 'model'}}</h2> 
 
<h3>Color: {{selectedCar | jsonFilt : 'color'}}</h3> 
 

 
<h1>You selected: {{selectedCar}}</h1> 
 
<h2>Model: {{selectedCar}}</h2> 
 
<h3>Color: {{selectedCar}}</h3> 
 

 
<p>The visible text inside the dropdown list can also be a property of the value object.</p> 
 

 
</div>

+0

非常感謝。與過濾器也是一個選項。我只是想了解ngOption和ngRepeat之間的區別。再次感謝。 – user75486