0

I have a code in angularjs which loops an ajax json response like this:

<div ng-repeat="post in posts">
{{post.title}}
{{post.url}}
</div>

It works fine. How do I do to pass a PHP variable based on the json?

Lets say the json looks have a structure this:

post
    my_slug
    my_title
post
    my_slug2
    my_title2

I have a PHP array like this:

my_slug
    url
my_slug2
    url2

my_slug in PHP matches my_slug in json.

Ajax call

var app = angular.module("MyApp", []);
app.controller("PostsCtrl", function($scope, $http) {
    $http.get('<?php echo u(); ?>test/json.txt').
    success(function(data, status, headers, config) {
        $scope.posts = data;
    }).
    error(function(data, status, headers, config) {
    });
});

The result should look somethins like this:

<div>
    my_title
    url
</div>
<div>
    my_title2
    url2
</div>

In the angularjs json loop I want to get url and url2 from my PHP array. How? Can I pass the PHP array with my Ajax response?

4
  • You question is not clear! Make it more concrete. Commented Jun 10, 2014 at 11:21
  • I also added a "result" of how it could look like when looping from ajax json and PHP array. Commented Jun 10, 2014 at 11:27
  • You lost me here Jens, if you use $http.get to call PHP script and it does return a well formatted JSON then it is straight forward how to use it in ng-repeat. Please explain further what is it that you want to achieve Commented Jun 10, 2014 at 11:37
  • Show how the actual variables really look like, from your indentation it's not clear if it's a key or value. Commented Jun 10, 2014 at 12:36

1 Answer 1

0

assign your php data to a variable then loop through the data to manipulate it before you assign it.

 $scope.postsPhp = [{$postsJSON}]; /// I usually make the php arrays json

 success(function(data, status, headers, config) { for (var i = 0; i <
 data.length; i++) { 
   data[i].title = data[i].url + ' ' + $scope.postsPhp[i].url; }   

$scope.posts = data; });

1
  • 1
    he doesn't have to join those two strings to display them, he already have {{post.url}} in the html Commented Jun 10, 2014 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.