Pass php variable/array data to angular js

Share fun

I am using php database connectivity with "$row" as an array that stores name as a field in it .But after lots of searching i dint get how to pass php variable in angular js here's the code ,I am trying to achieve.how is it possible i am new to this your help will be appreciated.

<?php

$con = mysqli_connect('localhost','root','','practice');

if (!$con) {
    die("couldnt connect". mysqli_error);
}

$q= "select * from test";
$result= $con->query($q);

if ($result->num_rows>0) {

    while ($row = $result->fetch_assoc()) {

        echo $row['name']."<br>";

    }
}

print_r($result);

echo json_encode($result);

?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js">
</head>
<body>

<ul ng-init="names = <?php echo htmlspecialchars(json_encode($row)); ?>">
<li ng-repeat="x in names">
   <p>{{x.name}}</p>
</li>
</ul>
</body>
</html>
Munanshu Madaan

Your php code needs a bit of improvement use this instead of that:

$arr = array();
if ($result->num_rows>0) {

while ($row = $result->fetch_assoc()) {

$arr[] = $row['name'] ;

}
}

Then echo it as

<ul ng-init="names = <?php echo htmlspecialchars(json_encode($arr)); ?>">

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass Angular JS Data to Button OnClick Event

From Dev

Pass Angular JS Data to Button OnClick Event

From Dev

Pass data from JS to PHP via XmlHttpRequest

From Dev

express js - pass backend vars/data to angular js

From Dev

Fetching array data of php in angular js and not able to update data in php

From Dev

How do I pass PHP variables to angular js?

From Dev

Angular JS pass data to $http a global error handler

From Dev

angular.js ui-router pass no data

From Dev

How to pass data from factory to controller in angular js?

From Dev

How to pass data from one page to other page in angular js

From Dev

ngroute pass different data for different id in angular js

From Dev

How to pass data from one page to other page in angular js

From Dev

How to pass json data from C# controller to angular js?

From Dev

angular.js ui-router pass no data

From Dev

Angular JS pass data to $http a global error handler

From Dev

how to pass data from service to directive in angular.js

From Dev

ngroute pass different data for different id in angular js

From Dev

Pass Json data from angular JS to spring REST API and handling it

From Dev

How to pass json data through ajax in Angular Js

From Dev

Pass data from js to php and download created csv file

From Dev

Trying to pass data from a .php file to a .js file

From Dev

Pass data to ngDialog in Angular

From Dev

Pass data to php in DropzoneJS

From Dev

Pass variable to PHP with JS

From Dev

Using angular.js to send data to a php file

From Dev

Can not bind data on view using angular.js and PHP

From Dev

How To use Php session variable as Angular Js Data

From Dev

combining angular js and php - failed insert data into database

From Dev

Unable to fetch JSON Data using PHP & Angular JS

Related Related

  1. 1

    Pass Angular JS Data to Button OnClick Event

  2. 2

    Pass Angular JS Data to Button OnClick Event

  3. 3

    Pass data from JS to PHP via XmlHttpRequest

  4. 4

    express js - pass backend vars/data to angular js

  5. 5

    Fetching array data of php in angular js and not able to update data in php

  6. 6

    How do I pass PHP variables to angular js?

  7. 7

    Angular JS pass data to $http a global error handler

  8. 8

    angular.js ui-router pass no data

  9. 9

    How to pass data from factory to controller in angular js?

  10. 10

    How to pass data from one page to other page in angular js

  11. 11

    ngroute pass different data for different id in angular js

  12. 12

    How to pass data from one page to other page in angular js

  13. 13

    How to pass json data from C# controller to angular js?

  14. 14

    angular.js ui-router pass no data

  15. 15

    Angular JS pass data to $http a global error handler

  16. 16

    how to pass data from service to directive in angular.js

  17. 17

    ngroute pass different data for different id in angular js

  18. 18

    Pass Json data from angular JS to spring REST API and handling it

  19. 19

    How to pass json data through ajax in Angular Js

  20. 20

    Pass data from js to php and download created csv file

  21. 21

    Trying to pass data from a .php file to a .js file

  22. 22

    Pass data to ngDialog in Angular

  23. 23

    Pass data to php in DropzoneJS

  24. 24

    Pass variable to PHP with JS

  25. 25

    Using angular.js to send data to a php file

  26. 26

    Can not bind data on view using angular.js and PHP

  27. 27

    How To use Php session variable as Angular Js Data

  28. 28

    combining angular js and php - failed insert data into database

  29. 29

    Unable to fetch JSON Data using PHP & Angular JS

HotTag

Archive