Angularjs& PHP JSON / ng-init / ng-repeat

时间:2016-05-16 14:29:49

标签: php angularjs json

我试图从返回php json结果重复ng.But我有一些错误代码=)

angular.min.js:117错误:[$ parse:ueoe] ** 抱歉我的英文。

JSON结果:

[{"id":"1","fullname":"Ad Soyad","email":"test@windowslive.com","tel":"12332123","message":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.","time":"12/05/2016","picture":"assets/admin/layout3/img/avatar1.jpg","isread":"1"},
{"id":"2","fullname":"Mahmut Tuncer","email":"test@windowslive.com","tel":"1233211","message":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.","time":"12/05/2016","picture":"assets/admin/layout3/img/avatar1.jpg","isread":"1"}]

a.php只会

$datacontactform = $sorgu = $dbquery->multisorgu('contactform');
   <ul  ng-init="contactlist = <?php echo $datacontactform; ?>">
    <li ng-repeat="data in contactlist"> {{data.fullname}}</li>
   </ul>

b.php

public function multisorgu($tablo,$where = null, $orderby = null, $limit = null){
    // MYSQL JSON RESULT    
    global $Baglan;
    $sorgu = mysqli_query($Baglan, "SELECT * FROM $tablo $where $orderby $limit");
    $result = array();
    if(mysqli_num_rows($sorgu) > 0){
        while($bak = mysqli_fetch_assoc($sorgu)){
            $result[] = $bak;
        }
    }
    //UTF-8 JSON_UNESCAPED_UNICODE
    $json_response = json_encode($result,JSON_UNESCAPED_UNICODE);
    return $json_response;
}

3 个答案:

答案 0 :(得分:0)

<强> 1。为什么会出错:

b

此行会触发错误,因为它喜欢x,但除了喜欢<ul ng-init="contactlist = <?php echo $datacontactform; ?>"> 之外的ng-init。

<强> 2。溶液

如果使用角度方式, Ajax 是一个很好的选择。

如果没有,试试这个:

ng-init=" contactlist = somestring "

任何问题都让我知道了。希望它有效。

答案 1 :(得分:0)

我假设您在PHP变量中获取JSON字符串而不是Object。您可以使用 JSON.parse 方法将字符串解析为对象&amp;如果您从PHP获得有效响应,它应该可以工作。在下面的示例中,我只是在Controller中硬编码JSON对象以供参考。

<!DOCTYPE html>
<html ng-app="mainApp" lang="en">
<head>
  <title>JSON PHP</title>
<script src="angular.min.js"></script>
</head>
<script>

     var app = angular.module('mainApp', []);
     app.controller('MyCtrlr', function ($scope, $http) {
                    //Store your JSON Response
                                 $scope.jsonResp = [{"id":"1","fullname":"Ad Soyad","email":"test@windowslive.com","tel":"12332123","message":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.","time":"12/05/2016","picture":"assets/admin/layout3/img/avatar1.jpg","isread":"1"},
{"id":"2","fullname":"Mahmut Tuncer","email":"test@windowslive.com","tel":"1233211","message":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.","time":"12/05/2016","picture":"assets/admin/layout3/img/avatar1.jpg","isread":"1"}];

                       //});

 });
</script>

<body>
   <div ng-controller="MyCtrlr">
         <ul  ng-init="contactlist = jsonResp">
            <li ng-repeat="data in contactlist"> {{data.fullname}}</li>
         </ul>
    </div>
</body>

</html>

答案 2 :(得分:0)

谢谢你的帮助。但我无法工作

我尝试这种方式,但现在我不能用ng-repeat工作这个json ..

<script type="text/javascript">
app.factory('jsonparse',function(){
    datacontactform = JSON.parse('<?php echo $datacontactform ?>'); 
    return datacontactform;
})

 mycontroller:

 $scope.cd = datacontactform;
    console.log($scope.cd);

控制台结果:

Object {1: Object, 2: Object}
1:Object
email:"tester@windowslive.com"
fullname:"tester tester"
id:"1"
isread:"1"
message:
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat."
picture:"assets/admin/layout3/img/avatar1.jpg"
tel:"123123123"
time:"12/05/2016"
__proto__:
2:Object
email:"tester@windowslive.com"
fullname:"tester tester"
id:"1"
isread:"1"
message:
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat."
picture:"assets/admin/layout3/img/avatar1.jpg"
tel:"123123123"
time:"12/05/2016"
__proto__: