【问题标题】:How to get object from ng-repeat using angular.fromJson()如何使用 angular.fromJson() 从 ng-repeat 获取对象
【发布时间】:2017-06-22 12:10:42
【问题描述】:

如果我使用 ng-option 我可以从选择值中获得一个对象, 但是,如果我在选项指令上使用 ng-repeat,我只能得到字符串。 我正在尝试使用 angular.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 }} 值?

在选择列表中,选项值为:

<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 字符?!?

非常感谢。

【问题讨论】:

    标签: angularjs json angularjs-ng-repeat ng-options angularjs-ng-options


    【解决方案1】:

    您缺少 ng-change :

    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>

    【讨论】:

    • 太棒了!非常感谢你!我错过了 ngChange 指令。再次感谢 ;-)
    【解决方案2】:

    另一种替代解决方案是创建自定义过滤器,将字符串转换为对象并返回相关属性

    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>

    【讨论】:

    • 非常感谢。带过滤器也是一种选择。我只是想了解 ngOption 和 ngRepeat 之间的区别。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多