How to replace currency symbol in angularjs - angularjs

I want to show one more currencies in a page. For example
$100,000 And 100,000€
When I use $filter('currency')(100000, '€') it returns €100,000. But I want it as 100,000€
The problem is I cannot replace the symbol. Any idea ?

You can always create a custom filter.
app.filter('customCurrency',['$filter', function(filter) {
var currencyFilter = filter('currency');
return function(amount, currencySymbol) {
var value = currencyFilter(amount).substring(1);
var currency = "";
switch(currencySymbol) {
case '$':
currency = currencySymbol + value;
break;
case '€':
currency = value + currencySymbol;
break;
}
return currency;
}}])
Here is the working example:
http://plnkr.co/edit/IIWG18?p=preview

Related

angularjs $translate : get default translation in error handler

I have written this factory which will be called in case of any errors
app.factory('customTranslationHandler', function ($translate) {
return function (caption, uses) {
if(uses=='en') {
var i = 0, strLength = caption.length;
for(i; i < strLength; i++) {
caption = caption.replace("_", " ");
}
var defaultText = caption.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
} else {
//var defaultText = $translate(caption).use('en');
//var defaultText = $translate.instant(caption).use('en');
}
return defaultText;
};});
If it is en, I format the caption and return it.
In case of any other language, I want to call translate for that caption using en as language. I get my translations from json files.
All I had to do was set fallBackLanguage:
$translateProvider.fallbackLanguage('en');

how to display Json Formated Datetime in angularJs

how to display Json datetime formate in angularJs its showing Datetime as"/Date(820434600000)/"
Angular Code
app.controller("MyDeptCntrl", function ($scope, MyDeptSer) {
$scope.BtnDept = function () {
var Dept = MyDeptSer.GetDeptData();
Dept.then(function (d) {
$scope.DeptData = d.data;
// $filter('date')(date, format, timezone)
},function(e){
alert('Loading Failed....')
})
}
use below function to parse the date first
function dateParser(input){
input = input.replace('/Date(', '');
return new Date(parseInt(input,10));
}
so
$scope.DeptData = dateParser(d.data);
You can try this
convertJsonDateTimeToJs: function (jsonDate) {
var dateSlice = jsonDate.slice(6, 24);
var milliseconds = parseInt(dateSlice);
return new Date(milliseconds);
}
I'd recommend changing your server side code to use a friendlier JSON serializer but if not, try:
// You start with /Date(820434600000)/
// substr(6) takes off the /Date( part and pass the number 820434600000 into the parseInt function.
// Note: the parseInt function will ignore the )/ and the end.
var friendlyDate = new Date(parseInt($scope.DeptData.someDateMember.substr(6)));
// Then, you can format it using angular date filter -- for example:
$scope.formattedDate = $filter('date')(friendlyDate, 'MM/dd/yyyy');

In angularJS, insert information into function property

This is the function I made:
var _loadNieuws =
function (url) {
switch (url) {
case 'example1':
_nieuws = EXAMPLE1_NIEUWS;
break;
case 'example2':
_nieuws = EXAMPLE2_NIEUWS;
break;
}
}
Now I'm trying to give the url a value using my controller, this is how far I came: NieuwsService.loadNieuws.url = 'example1';
But then I get this error:
"Cannot set property 'url' of undefined".
This is my whole factory:
App.factory('NieuwsService', ['EXAMPLE1_NIEUWS', 'EXAMPLE2_NIEUWS', function (EXAMPLE1_NIEUWS, EXAMPLE2_NIEUWS) {
var nieuwsService = {};
var _nieuws = [];
var _loadNieuws =
function (url) {
switch (url) {
case 'example1':
_nieuws = EXAMPLE1_NIEUWS;
break;
case 'example2':
_nieuws = EXAMPLE2_NIEUWS;
break;
}
}
nieuwsService.loadNiews = _loadNieuws;
nieuwsService.nieuws = _nieuws;
return nieuwsService;
}]);
So my question is how do I give the property url in the function a value using my controller ?
Where is the question?
If you want to pass variable into the function put it as a parameter in function:
<div ng-click="_loadNieuws(loadNieuws.url)">Click here</div>
and your js-code will work fine
I think you need something like this in your controller :
$scope.url = NieuwsService.loadNieuws('example1');
This is the solution that worked fine for me:
var url = 'example1';
$scope.url = angular.copy(NieuwsService.loadNieuws(url));

AngularJS filter for multiple strings

I'm working through the AngularJS tutorial, and understand the basics of
However, the out of the box implementation seems limited to just filter the list of items to the exact word or phrase entered in .
Example: if the query is "table cloth", the result list can include a result with this phrase, "Decorative table cloth", but won't include "Decorative cloth for table" because the filter is just a continuous search string.
I know there's the ability to add custom filters, but at first glance it seems like those are mainly transforms.
Is there any way to add a custom filter so that both "Decorative cloth for table" and "Decorative table cloth" show up in the filtered result set?
Some improvements to the above custom filter:
Instead of using a loop within a loop, counts, and indexOf, this one uses regular expressions to achieve a logical AND and also a logical OR depending on the third argument to the filter (input array of strings, search terms, AND or OR).
Have a look at the forked Fiddle with the two types of filter and results:
http://jsfiddle.net/jonjon/Cx3Pk/23/
angular.module('app', [])
.filter("myFilter", function () {
return function (input, searchText, AND_OR) {
var returnArray = [],
// Split on single or multi space
splitext = searchText.toLowerCase().split(/\s+/),
// Build Regexp with Logical AND using "look ahead assertions"
regexp_and = "(?=.*" + splitext.join(")(?=.*") + ")",
// Build Regexp with logicial OR
regexp_or = searchText.toLowerCase().replace(/\s+/g, "|"),
// Compile the regular expression
re = new RegExp((AND_OR == "AND") ? regexp_and : regexp_or, "i");
for (var x = 0; x < input.length; x++) {
if (re.test(input[x])) returnArray.push(input[x]);
}
return returnArray;
}
});
Please see surfbuds answer below as it is superior
Just roll with your own filter:
.filter("myFilter", function(){
return function(input, searchText){
var returnArray = [];
var searchTextSplit = searchText.toLowerCase().split(' ');
for(var x = 0; x < input.length; x++){
var count = 0;
for(var y = 0; y < searchTextSplit.length; y++){
if(input[x].toLowerCase().indexOf(searchTextSplit[y]) !== -1){
count++;
}
}
if(count == searchTextSplit.length){
returnArray.push(input[x]);
}
}
return returnArray;
}
});
jsfiddle: http://jsfiddle.net/Cq3PF/
This filter makes sure that all search words are found.
Alternatively you could use the default Angular filter within your custom filter like so:
angular.module('app').filter("multiWordFilter", function($filter){
return function(inputArray, searchText){
var wordArray = searchText ? searchText.toLowerCase().split(/\s+/) : [];
var wordCount = wordArray.length;
for(var i=0;i<wordCount;i++){
inputArray = $filter('filter')(inputArray, wordArray[i]);
}
return inputArray;
}
});
This could be embellished further with user2005009's AND_OR comparator.
Here's my version. It uses JonoWilko's method of using the built in filterFilter combined with surfbud's AND/OR flag (defaulted to "AND").
JavaScript
angular.module('filters').filter('searchFilter', function($filter) {
return function(inputArray, searchText, booleanOp) {
booleanOp = booleanOp || 'AND';
var searchTerms = (searchText || '').toLowerCase().split(/\s+/);
if (booleanOp === 'AND') {
var result = inputArray;
searchTerms.forEach(function(searchTerm) {
result = $filter('filter')(result, searchTerm);
});
} else {
var result = [];
searchTerms.forEach(function(searchTerm) {
result = result.concat($filter('filter')(inputArray, searchTerm));
});
}
return result;
};
});
CoffeeScript
angular.module('filters').filter 'searchFilter', ($filter)->
(inputArray, searchText='', booleanOp = 'AND')->
searchTerms = searchText.toLowerCase().split(/\s+/)
if booleanOp is 'AND'
result = inputArray
searchTerms.forEach (word)->
result = $filter('filter')(result, word)
else
result = []
searchTerms.forEach (word)->
result = result.concat $filter('filter')(inputArray, word)
result
'AND' Usage (default)
<div ng-repeat="product in products | searchFilter: searchInputText"></div>
'OR' Usage
<div ng-repeat="product in products | searchFilter: searchInputText : 'OR'"></div>
You can do a multiple word search on a object as follows:
.filter("myFilter", function(){
return function(input, searchText){
var returnArray = [];
var searchTextSplit = searchText.toLowerCase().split(' ');
for(var x = 0; x < input.length; x++){
var count = 0;
for(var y = 0; y < searchTextSplit.length; y++){
angular.forEach(input[x], function(item){
if(item.toLowerCase().indexOf(searchTextSplit[y]) !== -1){
count++;
}
});
}
if(count == searchTextSplit.length){
returnArray.push(input[x]);
}
}
return returnArray;
}
});
Working demo in js fiddle
Not a one liner but still quite short and fun
app.filter("filterall",function($filter) {
return function(arr,t){
(t?t.split(/\s+/):[]).forEach(function(v){ arr = $filter('filter')(arr,v); });
return arr;
};
});

combo box formula item and value

I have the following formula in a Combo Box:
var keyObj = getComponent('ACConditionToggle');
var key = keyObj.getSubmittedValue();
if (!key || key==''){
key = keyObj.getValue();
}
switch(key)
{
case 'Approval':
return ['% Approval' , 'Approvers']
break;
case 'Denial':
return ['% Denial', 'Deniers']
default:
return new Array();
}
It works fine, however, I want to have labels different from the value. SO in this case with the label '% Approval' I want a value of 'Percent' and for for 'Approvers' the value of 'Number'
So how do I pass the label and the value from a formula. I can do that with static and get itemLabel and itemValue but how do I differential them in the formula?
after beating my head against the wall I found the answer to be so simple.
var keyObj = getComponent('ACConditionToggle');
var key = keyObj.getSubmittedValue();
var rtnArray = new Array();
if (!key || key==''){
key = keyObj.getValue();
}
switch(key)
{
case 'Approval':
rtnArray[0]="% Approval|Percent";
rtnArray[1]="Approver(s)|Number";
return rtnArray;
break;
case 'Denial':
rtnArray[0]="% Denial|Percent";
rtnArray[1]="Denials(s)|Number";
return rtnArray;
break
default:
return new Array();
}

Resources