0

ng-optionを使用するとオブジェクトの選択値から得ることができます しかし、ng-repeat on optionディレクティブを使用するとただの文字列。 は私がangular.fromJson()でオブジェクトへのngのリピートから文字列を変換しようとしていますが、私は失敗しました: 私のコードは次のとおりです。angle.fromJson()を使用してng-repeatからオブジェクトを取得する方法

<!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カバラクタ?!?

ありがとうございました。

答えて

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

関連する問題