0

I have a AngularJS template where I embed dashboards. With PHP I generate some code. The first time I visit the page in my AngularJS template all is working great. But the second time I visit the page I see the old PHP code.

AngularJS is not regenerating the PHP code which should be changing every time you visit it. How could I solve this?

I need my PHP code to generate a unique ticket to embed the dashboard properly.

I have tried this post: AngularJs: Reload page but it is not working.

Thanks.

3
  • What do you mean by 'visit a page'. I take that it's about changing views not reloading? Commented Dec 8, 2015 at 20:15
  • Sounds like a caching issue. AngularJS has no control over your PHP scripts. Commented Dec 8, 2015 at 21:36
  • You might be caching your template. If you have a complete template regenerated by PHP then it is better to inject the template into a placeholder after $http request to get the template. If it is just a unique identifier string you can get that using a $http request to get the unique ticket. docs.angularjs.org/api/ng/service/$templateCache or disable template cache opensourcesoftwareandme.blogspot.in/2014/02/… Commented Dec 9, 2015 at 5:14

1 Answer 1

2

AngularJS works in a way different to how you are thinking it works. Give your head a shake and prepare to think a little differently.

In an AngularJS app, the displayed HTML or DOM, a mix of tags and business data, is generated client-side (browser). It should not be generated server-side with PHP, as you are trying to do.

AngularJS creates the displayed HTML client-side based on your AngularJS code/instructions and data. Your code/instructions include Javascript and the AngularJS template. Templates should not contain business data, but only instructions on where the data should be placed to create the HTML (or DOM) that is displayed. These instructions are ng-bind, {{var}}, ng-repeat, etc.

Just as it does not repeatedly retrieve your Javascript code/instructions from the server, AngularJS does not repeatedly retrieve your template code/instructions from the server. It gets it once, caches it locally, and re-uses it whenever it needs to re-display that page.

Your AngularJS templates really should be static *.html files, just like your .js (and .css) are static files. Only under some really weird requirement should you ever need to dynamically generate AngularJS templates with PHP.

(Now, to get the business data needed by the template, you use AngularJS's $http service. This makes a call to your PHP server-side. Your PHP code returns just the data, wrapped in JSON, ... and NOT wrapped in HTML.)

Sign up to request clarification or add additional context in comments.

2 Comments

This is true. Nice point BNCoder. You might want to move from php generated code to RESTAPI based code.
Thanks for the explanation, that really clears my mind. I use

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.