PHP echo to pass variable -- why doesn't this work?

2,228 views
Skip to first unread message

rlrh...@gmail.com

unread,
Aug 16, 2012, 12:57:55 AM8/16/12
to ang...@googlegroups.com
I need PHP to give me data from a database server, and I am trying to us an Angular controller to pass the model/data to the view. To test it, I have written the following code (note that I did not include the php for accessing the database, I am just trying to pass a string).  I am not sure why this isn't working. 

In my index.php page I have:

<!DOCTYPE HTML>

<html ng-app>

    <head>

        <title>Test of Angular/PHP</title>

        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>

        <script type="text/javascript" src="controllers/task_list_controller.js"></script>

    </head>

    <body>

        <h1>Tasks in Task Table</h1>

        <div ng-controller="taskListCtrl">

            <ul>

                <li>

                    This is using a controller: {{tasks}}

                    <br />

                    This is using the direct php:

                    <?php echo 'test'; ?>

                </li>

            </ul>

        </div>

    </body>

</html>

in the task_list_controller.js file I have:

function taskListCtrl ($scope) {

$scope.tasks = <?php echo 'test';?>

}


Why doesn't his work?  What might I be doing wrong? What do I need to do so that I can pass data from PHP to an angular controller?


Thanks,

Al...@enplug.com

unread,
Aug 16, 2012, 1:26:48 AM8/16/12
to ang...@googlegroups.com, rlrh...@gmail.com
What is the output of the PHP script going to be? What does the output look like in the script once it's loaded? (view source)

If you're outputting a string then unless the PHP variable you're outputting has quotation marks and a semi-colon included, it looks like your output may end up like:

$scope.tasks = my_var_value

But to be a valid javascript variable (with string content) it needs to be:

$scope.tasks = 'my_var_value';

Alex Ross

unread,
Aug 16, 2012, 1:29:46 AM8/16/12
to ang...@googlegroups.com, rlrh...@gmail.com
Sorry in my reply above I forgot to mention that it appears you're also trying to evaluate PHP in a javascript (*.js) file..? Unless your server has non-standard configs set up you need to put that code in a .php or .phtml file.


On Wednesday, August 15, 2012 9:57:55 PM UTC-7, rlrh...@gmail.com wrote:

rlrh...@gmail.com

unread,
Aug 16, 2012, 11:38:47 AM8/16/12
to ang...@googlegroups.com, rlrh...@gmail.com
FILE EXTENSION!  You got it.  Thanks. Following the tutorials I had put the controller in a .js file.  Of course it wouldn't evaluate the php.

Thanks!

Will Kriski

unread,
Aug 16, 2012, 12:02:20 PM8/16/12
to ang...@googlegroups.com, rlrh...@gmail.com
Another way to think about it might be to pull the data into the array from the php server via an angular service using $resource ($http) rather than injecting php code into the js files. Then the array could be stored locally say in localstorage so the app could work offline if needed. Just a thought.
Reply all
Reply to author
Forward
0 new messages