Angular translate provider - Is there a way to replace general variables? - javascript

In my app, many of my translation string need to include the user name in them.
For example: Hello {{user_name}}!
(User name has to be part of the translation string since the position of the name in the string depends on the language))
The way to set attributes is {{"TRANSLATE_ID" | translate:{user_name:myUserName}}}
Since {{user_name}} appears in more than 100 translation strings, I don't want to send the user_name parameter so many times. I'd like to have a way to set this parameter only one.
I can't replace the {{user_name}} string in the config because the user_name is set asynchronously (fetched from the server) and isn't available when translateProvider sets the strings.
Thanks

AngularTranslate allows you to translate tags from the html itself or inside the controller through a promise. I believe the elegant way to do what you want is to create a method/function in your controller that handles the translation in the way you want and then call this method/function in your html passing the username as a parameter.
Address the username in your JSON translation files as "USERNAME" for instance, such as "Hello, USERNAME!", do the translation in a method created in your controller using AngularTranslate and then do a string replacement changing the word USERNAME for the method parameter (the real username) and use this string as the method's return value, which will be called in your html.
Hope this helps.

Related

How to substring variable and concatenate it with url in Thymeleaf

I want to substring a variable which is my JPA class id field and then add it to an URL in Thymeleaf.
My URL is like
/Myapplication/sortddoc/value=__${entity.id}__
So I need to do something like
${entity.id}.substr(0, 8)
before concatenating it to the URL.
I tried to create a new Transient field in my entity class containing the substring variable but it doesn't work because it seems like to need database field that I cant provide.
Can Anyone help me, please ?
You should be doing this with Thymeleaf's standard URL syntax, instead of concatenating string variables or using preprocessing (there is no need for either of those).
<a th:with="${value=#strings.substring(entity.id,0,8)}"
th:href="#{/Myapplication/sortddoc/(value=${value})}"></a>
or
<a th:href="#{/Myapplication/sortddoc/(value=${#strings.substring(entity.id,0,8)})}"></a>

Getting string index value from Javascript to MVC C# Controller using string[] array as a parameter

I am new in this environment and i just want to ask somebody if it is possible to get the string value not the index value using string[] array as a parameter?
Below are those images: ajax pass this data into controller
I am using ajax to pass my data in to the url controller in c# mvc.
By the way, here's my sample array data: prepared data..
the highlighted one is my array and in my parameter in mvc # is declared as string [] methodParam: highlighted parameter,
Those not highlighted parameter are working. all i want to do is getting one by one string in methodParam. i tried to use this
GetMethodParam.IndexOf("Date").ToString() but the output is -1 which probably not available in context.
i just want to get each string and its value because i send it to the email outlook..
like this. enter image description here.
Any Suggestions, clarification or comments is highly appreciated. Thank you ;) .
You don't need the individual input in data on ajax call, just use the last one UserHeader
and in Acton(C#) use a model as argument with containing all attributes of UserHeader.
Or, remove UserHeader from data and Use each attributes as individual argument in C# Action with same name.

Mathematical operation on angular template

Whenever I a try something like this in view template it is working as expected.
{{20*30}}
but whenever I try
{{somevalue1*somevalue2}}
Where somevalue1 and somevalue2 both are coming from component and somevalue2 is coming from api and it is inside an array, it is displaying NaN.
How to deal with that?
Is it because of the String value?
How to convert that in Number in template?
Is it because of the String value?
Yes, it is. You can't easily access the windows scope in template scope, so I would make a method:
calculateStuff(){
return Number(somevalue1) * Number(somevalue2);
}
And in template:
{{calculateStuff()}}
If the string doesn't start with characters representing number, it will still return NaN.
somevalue1 somevalue2 are probably strings. You'll need to share more code to get a real answer, though.
Add {{somevalue1}} and {{somevalue2}} to the template as well, just so you can be sure the template is receiving the values you expect.
Problem is the string value that is formatted according to some locale ( in your case it is US locale )
You can eighter:
convert the strings to values on data retrieving
create angular transform pipe
use a helper function/method in your template
if you are able, adjust data on the server side (it is a good practice sending raw data and formatting them when needed)
There are several ways how to handle the conversion. If this is the only format you receive that numbers. Simply replace the comma Number('1,234.42'.replace(',', '')) or use some of the libs that were created for this purpose like NumeralJS

Is there a way to determine in angular.js if there are url query parameters present?

In angular routes, you can parse the url arguments into $routeParams. However, I need to know what's the best way to see if they exist.
For example, I have several $routeParams from different parts of the URL however, all I want to know is whether any url params exist (if the ? exists and parameters after). I understand, I can just do a search for the ? but wanted to know if there was a way within angular.
Here's my code:
$routeProvider.when('/sports/:sport_id/:args?',
sport_id and all the args will be combined in $routeParams. However I just want to know if args exist. (the query parameters)
You can inject $routeParams and look for the presence of any properties in the object.
ex: var hasParams = Boolean(Object.keys($routeParams).length);
You dont need the Boolean( part as 0 will be falsy anyways, however just being more explicit.
If looking for just the query string params you can just inject $location service and use $location.search() it will provide object with query string argument as key value pair on the object.

How Do You Fix A Parameter Names Mismatch - DOJO and PL/SQL

How do you fix a names mismatch problem, if the client-side names are keywords or reserved words in the server-side language you are using?
The DOJO JavaScript toolkit has a QueryReadStore class that you can subclass to submit REST patterned queries to the server. I'm using this in conjunction w/ the FilteringSelect Dijit.
I can subclass the QueryReadStore and specify the parameters and arguments getting passed to the server. But somewhere along the way, a "start" and "count" parameter are being passed from the client to the server. I went into the API and discovered that the QueryReadStore.js is sending those parameter names.
I'm using Fiddler to confirm what's actually being sent and brought back. The server response is telling me I have a parameter names mismatch, because of the "start" and "count" parameters. The problem is, I can't use "start" and "count" in PL/SQL.
Workaround or correct implementation advice would be appreciated...thx.
//I tried putting the code snippet in here, but since it's largely HTML, that didn't work so well.
While it feels like the wrong thing to do, because I'm hacking at a well tested, nicely written JavaScript toolkit, this is how I fixed the problem:
I went into the DOJOX QueryReadStore.js and replaced the "start" and "count" references with acceptable (to the server-side language) parameter names.
I would have like to handled the issue via my PL/SQL (but I don't know how to get around reserved words) or client-side code (subclassing did not do the trick)...without getting into the internals of the library. But it works, and I can move on.
As opposed to removing it from the API, as you mentioned, you can actually create a subclass with your own fetch, and remove start/count parameters (theoretically). Have a look at this URL for guidance:
http://www.sitepen.com/blog/2008/06/25/web-service-data-store/
Start and count are actually very useful because they allow you to pass params for the query that you can use to filter massive data sets, and it helps to manage client-side paging. I would try to subclass instead, intercept, and remove.
Is your pl/sql program accessed via a URL and mod_plsql? If so, then you can use "flexible parameter passing" and the variables get assigned to an array of name/value pairs.
Define your package spec like this...
create or replace package pkg_name
TYPE plsqltable
IS
TABLE OF VARCHAR2 (32000)
INDEX BY BINARY_INTEGER;
empty plsqltable;
PROCEDURE api (name_array IN plsqltable DEFAULT empty ,
value_array IN plsqltable DEFAULT empty
);
END pkg_name;
Then the body:
CREATE OR REPLACE PACKAGE BODY pkg_name AS
l_count_value number;
l_start_value number;
PROCEDURE proc_name (name_array IN plsqltable DEFAULT empty,
value_array IN plsqltable DEFAULT empty) is
------------
FUNCTION get_value (p_name IN VARCHAR) RETURN VARCHAR2 IS
BEGIN
FOR i IN 1..name_array.COUNT LOOP
IF UPPER(name_array(i)) = UPPER(p_name) THEN
RETURN value_array(i);
END IF;
END LOOP;
RETURN NULL;
END get_value;
----------------------
begin
l_count_value := get_value('count');
l_start_value := get_value('start');
end api;
end pkg_name;
Then you can call pkg_name.api using
http://server/dad/!pkg_name.api?start=3&count=3

Categories

Resources