0

I have integrated an Angular4 app into a WordPress plugin (so in some PHP code). In a view I call all the files I get by making a publish "ng build -prod"

<script type="text/javascript" src="{{PLUGIN_URL}}assets/js/inline.bundle.js"></script>
<script type="text/javascript" src="{{PLUGIN_URL}}assets/js/polyfills.bundle.js"></script>
<script type="text/javascript" src="{{PLUGIN_URL}}assets/js/vendor.bundle.js"></script>
<script type="text/javascript" src="{{PLUGIN_URL}}assets/js/main.bundle.js"></script>

My Angular app works very well into the PHP code. Now my problem is I need to pass an "id" from the PHP app to the Angular app. What will be the best solution / architecture?

Something like : "main.bundle.js?id=".myIdValue."

It would be better that this "id" stay hidden from the final user (so not displayed into the navigator URL...). How would you do that?

1 Answer 1

1

What you could do, is to set a global window variable with php, like

echo "<script>window.myIdValue = 'something';</script>";

In your angular application you can read this. Here is a good place, which shows you an example of best practice using global window variable in angular2+ :

https://medium.com/@laco0416/access-to-global-variables-in-angular-2-b2c395eac1d8

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

1 Comment

Thanks for your help. The script with "windows" value just have to be called before the other Angular .js in the PHP file and it works!

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.