0

Wenn ich die ng-Option benutze, kann ich von dem ausgewählten Wert ein Objekt bekommen, Aber, wenn ich die Option ng-repeat on Option verwenden kann nur Zeichenfolge. Ich versuche Zeichenfolge von ng-repeat zu konvertieren mit angular.fromJson() zu widersprechen, aber ich schlug fehl: mein Code:Wie bekomme ich ein Objekt von ng-repeat mit 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> 

wenn ich die Zeile:

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

An:

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

Es funktioniert!

Warum kann ich den Wert {{selectedCarObj.brand}} nicht erhalten?

auf der Auswahl lis Optionswert ist:

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

Kann mir jemand helfen zu verstehen, warum es nicht funktioniert?

Vielleicht html caharacters?!?

Vielen Dank.

Antwort

1

Sie fehlen ng-Änderung:

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

Great! Vielen Dank! Ich habe die ngChange-Richtlinie verpasst. Danke nochmal ;-) – user75486

0

eine andere alternative Lösung besteht darin, einen benutzerdefinierten Filter zu erstellen, um die Zeichenfolge zu konvertieren die relevante Eigenschaft zum Objekt und zurück

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

rufen Sie den Filter in der HTML wie folgt

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

Demo

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

Vielen Dank. mit Filter ist auch eine Option. Ich wollte nur den Unterschied zwischen ngOption und ngRepeat verstehen. Nochmals vielen Dank. – user75486

Verwandte Themen