问题
working on the sample meanjs application. I am not able to understand the below code snippet. Kindly explain the use of this script in layout.server.view.html view.
<!--Embedding The User Object-->
<script type="text/javascript">
var user = {{ user | json | safe }};
</script>
回答1:
Basically user
is being sent via express/swig everytime a request is made to the route /*
(see here) so that user data can be used by AngularJS.
If you check the file core.server.controller.js
(here), you will see that MEAN.JS passes a user object in the response in this code block:
res.render('modules/core/server/views/index', {
user: safeUserObject
});
Then in the code block you mentioned, you're assigning user object sent by express/swig to the variable user
which can then be accessed later using $window.user
similarly to what is done here.
json and safe are two swig filters. The first one returns a string representation of a JavaScript object and the second one forces the input to not be auto-escaped.
来源:https://stackoverflow.com/questions/37519300/what-is-the-use-of-the-script-mentioned