【问题标题】:Angularjs convert string to object inside ng-repeatAngularjs将字符串转换为ng-repeat中的对象
【发布时间】:2015-12-26 04:57:23
【问题描述】:

我的数据库中保存了一个字符串

{"first_name":"Alex","last_name":"Hoffman"}

我将它作为对象的一部分加载到作用域中,然后使用 ng-repeat 遍历它。范围内的其他值只是字符串

{"id":"38","fullname":"{\"first_name\":\"Alex\",\"last_name\":\"Hoffman\"}","email":"alex@mail","photo":"img.png"}

但我想在 ng-repeat 中使用 ng-repeat 将名字和姓氏分开

<div ng-repeat="customer in customers">    
    <div class="user-info" ng-repeat="name in customer.fullname">
       {{ name.first_name }} {{ name.last_name }}
    </div>
</div>

我什么也得不到。我认为,问题在于,全名值是一个字符串。是否可以将其转换为对象?

【问题讨论】:

    标签: json angularjs object scope


    【解决方案1】:

    我有同样的问题,但我通过自定义过滤器解决了这个问题......

    JSON:

    .........
    "FARE_CLASS": "[{\"TYPE\":\"Economy\",\"CL\":\"S \"},{\"TYPE\":\"Economy\",\"CL\":\"X \"}]",
    .........
    

    用户界面:

    <div class="col-sm-4" ng-repeat="cls in  f.FARE_CLASS | s2o">
       <label>
           <input type="radio"  ng-click="selectedClass(cls.CL)" name="group-class" value={{cls.CL}}/><div>{{cls.CL}}</div>
       </label>
    </div>
    

    自定义过滤器::

    app.filter("s2o",function () {
        return function (cls) {
            var j = JSON.parse(cls);
            //console.log(j);
            return j;
        }
    
    }); 
    

    【讨论】:

      【解决方案2】:

      首先...我不知道为什么该部分会存储为字符串...但我是来救你的。

      当您第一次获取数据时(我假设是通过 $http.get 请求)...在将其存储到 $scope.customers 之前...让我们这样做:

      $http.get("Whereever/You/Get/Data.php").success(function(response){
      //This is where you will run your for loop
        for (var i = 0, len = response.length; i < len; i++){
          response[i].fullname = JSON.parse(response[i].fullname)
          //This will convert the strings into objects before Ng-Repeat Runs
        //Now we will set customers = to response
         $scope.customers = response
      
       }
      })
      

      现在 NG-Repeat 旨在循环遍历数组而不是对象,因此您不需要嵌套的 NG-Repeat...您的 html 应该如下所示:

      <div ng-repeat="customer in customers">    
      <div class="user-info">
         {{ customer.fullname.first_name }} {{ customer.fullname.last_name }}
      </div>
      

      这应该可以解决您的问题:)

      【讨论】:

      • 是的,这行得通……但我认为 PDO 查询或 sql 中存在一些问题……我不明白,为什么我将它作为字符串获取。会努力解决的。如果没有 - 坚持你的建议
      【解决方案3】:

      我的建议是使用如下过滤器: &lt;div class="user-info"... ng-bind="customer | customerName"&gt;...

      过滤器看起来像:

      angular.module('myModule').filter('customerName', [function () {
          'use strict';
      
          return function (customer) {
            // JSON.parse, compute name, foreach strings and return the final string
          };
        }
      ]);
      

      【讨论】:

        【解决方案4】:

        您必须将字符串值转换为对象(为什么它是字符串,不知道)

        .fullname = JSON.parse(data.fullname); //replace data.fullname with actual object
        

        然后使用对象ngRepeat语法((k, v) in obj):

        <div class="user-info" ng-repeat="(nameType, name) in customer.fullname">
            {{nameType}} : {{name}}
        </div>
        

        【讨论】:

        • 我不知道它是否是一个字符串,但它不适用于 ng-repeat,所以我认为它是一个字符串。我是否必须遍历整个范围对象才能转换每个字符串值?不能在模板中完成吗?
        • @Sobakinet - 我想说第一步是找出这是否是一个实际的字符串 - 如果它是 not - 你需要使用正确的 ngRepeat 语法 -如果是,那么您可以创建一个filter 来转换它 - 但为什么不在客户端获取它之前通过服务器进行转换?
        • 我的问题中最后一个代码 sn-p 中的 ng-repeat 语法是否不正确?
        • @Sobakinet -- 这是数组的语法[1,2,3] - 对于一个对象,它是不同的{name:"Justin", number:3} - (k, v) in obj
        猜你喜欢
        • 2015-02-05
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多