P
P
pmcode2015-01-06 08:54:42
Angular
pmcode, 2015-01-06 08:54:42

Is it possible to use inline JS in Angular?

In continuation of this question. Here's a finished example: jsfiddle.net/nynqqfz1/6 . With child $scope everything works fine. The problem is that it is necessary for the server to give the contents of the menu in HTML, and not in a static JS file. Because that's how i18n and Spring Security tags work - the contents of the menu vary depending on the user's language settings and privileges.
The menu should be given directly to index.html and not requested through a separate Angular service, as an option.
Question: how to slip $scope.data = [] into the controller via Inline JS ?
PS They didn't ban me on Google, but they don't recommend anything sensible there.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-01-06
Protko @Fesor

How about this option:

<script>
angular.module('app').value('sideMenuData', {{ myData | json }});
</script>

Or whatever is more convenient.
<script>
angular.module('app.inlineData', []).value('sideMenuData', {{ myData | json }});
</script>

and in your application make a dependency on this module. This method is most convenient from the point of view that the application module will not exist by the time this code is executed.

S
Sergey Romanov, 2015-01-06
@Serhioromano

Because that's how i18n and Spring Security tags work - the contents of the menu vary depending on the user's language settings and privileges.

I don't see any disadvantages in this. If the server does it anyway, then it makes no difference whether it will generate ready-made HTML with only those elements that are possible, or it will generate JSON with only those elements that are needed. So is the translation. JSON and HTML are 2 text formats. No matter what format the server returns, it can also insert the translation into the JSON values.
I do not think that if your application works like this, then you need to use Angular. You could use just _ or jQuery for this. What I see is that you are trying to misuse the tool.
And the lack of loading templates generated on server 1 in Angular was widely discussed. And this is why React is now gaining popularity as it can use the HTML made by the server on the first load and then pick it up as a SPA.
For example, it's hard for me to tell you how to unscrew a Phillips screw with a flat screwdriver. Well, of course, you can saw through the cut, or pick it up by the edge like a pry bar, but ....
1 - I know that this can be implemented. We are talking about native support for templates pre-generated on the server for a quick start, and then continue as usual.

A
Alexey P, 2015-01-06
@ruddy22

The question, in this case, can have at least 2 solutions.
1. Using Angular - everything is simple
2. Not using Angular - everything is also very simple
Angular can be used partially and not build all templates through it, something can come from a
server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question