Si j'utilise l'option ng, je peux obtenir de la valeur de sélection un objet, Mais, si j'utilise la directive ng-repeat on option, je peux obtenir juste une chaîne. Je suis en train de convertir la chaîne de ng répétition à l'objet avec angular.fromJson() mais j'échoué: mon code est:Comment obtenir l'objet de ng-repeat en utilisant 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>
si je change la ligne:
$scope.selectedCarObj = angular.fromJson($scope.selectedCar);
Pour:
$scope.selectedCarObj = angular.fromJson({brand : "Ford", model : "Mustang", color : "red"});
Cela fonctionne! Pourquoi je ne peux pas obtenir la valeur {{selectedCarObj.brand}}?
sur la sélection lis la valeur de l'option est:
<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 -->
que quelqu'un peut me aider à comprendre pourquoi il ne fonctionne pas?
Peut-être html caharacters?!?
Merci beaucoup.
Great! Merci beaucoup! J'ai manqué la directive ngChange. Merci encore ;-) – user75486