AngularJS - need access in index.html page to data from a service - javascript

I need to place code on every page of my app in index.html (Google Analytics tracking code). The issue is that some of the code needs to pull values from an Angular service. I'm wondering what the best practice is for doing this?
I was thinking to have run block that uses an injected service to get the value. I could then set a constant that I would have access to in index.html. I'm not sure if that's possible or not.
Any ideas? Thanks.

I'm not sure I understand exactly what you are trying to do, but it sounds like its as simple as retrieving a value from a service and binding it to a tag on your page.
In your controller:
$scope.yourConstant = yourInjectedService.getValue();
Then in your index.html:
<span>{{ yourConstant }}</span>
or:
<span ng-bind='yourConstant'></span>
IF I've misunderstood please try to clarify your scenario.

Related

making router for parsing javascript request through php

I have seen this request
https://somesite.com/path/script.js?shop=xyz.com
https://somesite.com/path/script.js?shop=abc.com
the output for both requests are different
earlier I was thinking that .js file cant be treated like php and dynamic data cant be passed but I was wrong and some expert advised that its router which is passing these data and according to shop name they are serving.
I have php apache+ nginx server
can anyone tell how we can make this work. don't want to go with Nodejs as it won't allow to pass php code.
just for test i want to call above links and print this.
hi i am shop xyz
or hi i am shop abc
on respective calls.
can anyone share some details guidelines how to make router. any link any guide any advise will solv the purpose.
I have never dealth with such javascript ant it is damm easy in php extension but want to learn in javascript.

Can you refuse access to views with Pug/Jade conditionals?

For example, a verification email info page. Anybody can currently visit www.mysite.com/verified and see the thank you message. Is there any way I can use conditionals to stop people from being able to view this page?
Thanks.
In the controller you should render the correct template for authorized/unauthorized, utilizing the verification url id.
In the pug template, you could theoretically render or not render the template by simply using an if-statement on the entire template, but I personally wouldn't go about it this way.
- if (allowUser)
.body
.myhtml
- else
.body
.notallowed

Equivalent of Request.RawUrl for Javascript

I apologize if this a duplicate question. I spent quite a while searching for a similar question but was not able to find one.
I am looking to get the URL of the current PartialView in java-script (in an angular controller). So far, methods I have found all return the existing browser URL:
window.href.location;
window.href.pathname;
as well as the $location service method in angular. However, I'm looking for a method that returns the same value as the Razor's '#Request.RawUrl' (i.e. the actual Route/URL of the PartialView).
For example, on page '/Category/{categoryId}/Product' I am showing a list of products. I am then using ngDialog and ngInclude to load a PartialView from '/Category/{categoryId}/Product/{productId}/[Whatever]' in a Dialog page.
Then I fire an angular controller in the PartialView. In that controller, I'd like to use the $http service and a URL relative to the PartialView's url. However, this is not possible, since angular is not aware of the PartialView's URL (i.e. the URL that #Request.RawUrl produces if used in the PartialView).
Thank you.
example

How can I go to a complete State of angular app

I need to access the URL http://{myapp}/product/car/new, but I'm getting only the template for state car.new.....
I want to load the entire app and the car.new too
Ps.: It's like copying a URL a link of angular docs: https://docs.angularjs.org/api/ng/function/angular.injector for example.
You should take a look to $location, it will let you get the full url or the params, but it will be wiser for you to show us your code, because we cant know if you already tried this alternative

Use of sec:authorize to load javascript

I'm making a development using Spring MVC and thymeleaf.
I'm trying to use sec:authorize to load javascript. In other words, I only want this script to load when the user is authenticated.
Here is code I'm trying to get to work:
<script src="/js/jquery.min.js"
th:src="#{/js/jquery.min.js}"></script>
<script src="/js/submit.js"
th:src="#{/js/submit.js}"></script>
<script src="/js/url.js"
th:src="#{/js/url.js}"></script>
<!-- admin -->
<script sec:authorize="isAuthenticated()" src="/js/jquery.simplemodal-1.4.4.js"
th:src="#{/js/jquery.simplemodal-1.4.4.js}"></script>
<script sec:authorize="isAuthenticated()" src="/js/admin.js"
th:src="#{/js/admin.js}"></script>
These last 2 resources I'm trying to use sec:authorize to load but they seem to handle every time I load the page. Is this a valid way to use sec:authorize? Id it isn't, is there any way to do this?
isAuthenticated() may be returning ROLE_ANONYMOUS which is a legit role, hence why its returning true and rendering your script.
here is a good explanation why user can be authenticated by anonymous (which in turn breaks your logic) Why is the 'anonymousUser' authenticated in Spring Security?
To circumvent that and specifically advise you want user with specific access there is a need for roles such as ROLE_USER, ROLE_ADMIN.
Hope that helps.

Categories

Resources