Sob*_*net 2 json scope object angularjs
我的数据库中保存了一个字符串
{"first_name":"Alex","last_name":"Hoffman"}
Run Code Online (Sandbox Code Playgroud)
我将它作为对象的一部分加载到范围中,然后使用ng-repeat进行处理.范围中的其他值只是字符串
{"id":"38","fullname":"{\"first_name\":\"Alex\",\"last_name\":\"Hoffman\"}","email":"alex@mail","photo":"img.png"}
Run Code Online (Sandbox Code Playgroud)
但我想在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>
Run Code Online (Sandbox Code Playgroud)
我一无所获.我认为,问题是,全名值是一个字符串.是否可以将其转换为对象?
首先......我不知道为什么那部分会被存储为字符串......但我来这里是为了救你.
当你第一次得到数据时(我假设通过$ 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
}
})
Run Code Online (Sandbox Code Playgroud)
现在NG-Repeat被设计为遍历数组而不是对象,所以你的嵌套NG-Repeat不是必需的......你的html应该如下所示:
<div ng-repeat="customer in customers">
<div class="user-info">
{{ customer.fullname.first_name }} {{ customer.fullname.last_name }}
</div>
Run Code Online (Sandbox Code Playgroud)
这应该解决你的问题:)
| 归档时间: |
|
| 查看次数: |
20367 次 |
| 最近记录: |