Issues with php function calls inside ng-view

753 views
Skip to first unread message

Billy Figueroa

unread,
Mar 4, 2014, 2:28:54 PM3/4/14
to ang...@googlegroups.com

Hello, I have an issue when trying to call a function from a php template file that is being loaded via ng-view. This is only a problem inside the ng-view directive. If I make a function call from the main/shell page then it executes properly but if I make the same call from a template file being loaded with ng-view it does not work.

My best attempt to recreate this is to use jsfiddle and just paste the code in there. The preview will not work. I used the html section as if it was a .php file with some php code at the top. The css page I used as the php class where my function was defined and then the js file to show the basic angular usage.

Please let me know if this is a real issue or a limitation. I would love to get around this issue.

Here is the js fiddle link...
http://jsfiddle.net/figuer25/LjLm4/

Antoine Polatouche

unread,
Mar 4, 2014, 3:28:32 PM3/4/14
to ang...@googlegroups.com
Le 04/03/2014 20:28, Billy Figueroa a écrit :
>
> Hello, I have an issue when trying to call a function from a php
> template file that is being loaded via ng-view. This is only a problem
> inside the ng-view directive. If I make a function call from the
> main/shell page then it executes properly but if I make the same call
> from a template file being loaded with ng-view it does not work.
>
This is not related to AngularJS.
You have to understand how php and javascript are working: php scripts
are interpreted on the server side, when a php file is requested.
Javascript is interpreted on the client side.
So the template loaded by AngularJS can't be interpreted on the client side.

Your FiddelJS can't work, because a php script doesn't work in a html
file, neither in a css file...

This is not an issue nor a limitation.

If you need to use php scripts, take a look at
http://docs.angularjs.org/api/ng/service/$http. You can implement an
AngularJS service that is requesting php scripts and use this service in
your javascript code.

Billy Figueroa

unread,
Mar 4, 2014, 4:23:45 PM3/4/14
to ang...@googlegroups.com
Hi Antoine I think that clears things up for me. So my "shell" page is ok because that can be interpreted by the server, while the "template" is being interpreted by angular which is javascript client side code. So if I have this right...we can NEVER use php code inside of an angular js template that is being rendered with ng-view, or for that matter anything that is NOT html (i.e. python, ruby)?

I am familiar with services and factories. I have actually been using directives a lot in this website and have used $http.post plenty of times which on success I feed into my model and then use angular to display the data. However in this case I did not want to create a separate PHP script to do this, I wanted to do it like I was doing the old way (before using angular) which was to have php code executed from the index.php page to call a function (which I had access to by having the include statement at the top in php). The issue here is I had php code inside of an angular js loaded template, which even though I named "main.php", angular does not know how to render the php

I think the confusion was I was expecting angular to be able to display php and it cannot. Not only with php it probably can not output python or anything that is not html. Amiright?

Antoine Polatouche

unread,
Mar 4, 2014, 9:34:52 PM3/4/14
to ang...@googlegroups.com
Le 04/03/2014 22:23, Billy Figueroa a écrit :
> Hi Antoine I think that clears things up for me. So my "shell" page is
> ok because that can be interpreted by the server, while the "template"
> is being interpreted by angular which is javascript client side code.
Exact.

> So if I have this right...we can NEVER use php code inside of an
> angular js template that is being rendered with ng-view, or for that
> matter anything that is NOT html (i.e. python, ruby)?
Yes. The browser ask the server for a html content, the server side
scripts are interpreted by the server and the html result is sent to the
browser. At this time, the client side scripts are interpreted by the
browser and there is no way to execute server side scripts.

Billy Figueroa

unread,
Mar 5, 2014, 7:33:33 PM3/5/14
to ang...@googlegroups.com
Thanks, how do I put the post in "solved" state. Or do moderators do that?
Reply all
Reply to author
Forward
0 new messages