How to exclude empty spaces when counting length in Angular? - angularjs

I am checking the length of a title and setting a class, based on that length. I noticed though, that the length is also checking the empty spaces, so when you have a space, it would count as +1.
I want to get rid of that. So I want to exclude the count of empty spaces in the length. How can I do it, if it's possible?
<h1 ng-class="{'specific-class': title.name.length >= 10}">{{title.name}}</h1>

You could check this by replacing the spaces with empty strings:
title.name.replace(' ','').length >= 10
The full line would be:
<h1 ng-class="{'specific-class': title.name.replace(' ','').length >= 10}">{{title.name}}</h1>
Say if title.name was 'Hello World!', title.name.length would be 12, but title.name.replace(' ','').length is 11.
EDIT
Turns out you can't use slashes inside HTML or Angular will convert them to html-safe characters. I'd suggest therefore to separate the checker out into its own module. I've attached a snippet so you can see how it's done:
angular
.module("app", [])
.controller("test", function($scope) {
// Example title object. Just load title objects as you would normally.
$scope.title = {name: 'The World of McNamara'};
// The actual magic happens here:
$scope.checker = function(word) {
return word.replace(/\s/g, '').length >= 10;
}
})
.specific-class {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!-- snippet checks whatever is passed to it as title.name -->
<div ng-app="app" ng-controller="test">
<h1 ng-class="{'specific-class': checker(title.name)}">{{title.name}}</h1>
</div>

You could do in multiple ways to remove empty spaces from the string like using split and then join.
title.name.split(' ').join('')
Or could be done by regex approach:
title.name.replace(/ /g, '')
It is up to you how you want to implement.

You would have to use .trim :
title.name.trim().length
Or you could do :
title.name.replace(" ", "").length
EDIT :
In your code it would be like so :
<h1 ng-class="{'specific-class': title.name.replace(' ', '').length >= 10}">{{title.name}}</h1>
Using ternary logic :
<h1 ng-class="(title.name.replace(' ', '').length >= 10)? 'specific-class' : 'another-class' ">{{title.name}}</h1>

Related

ng-bind with AngularJS translate interpolation

I need to render characters such as < and >, which may be included in a user's first or last name. I'm using angular translate to render the name, passing the first and last name as values. However, if the first or last name contains < the value will not be displayed. I think I need to use something like ng-bind="translation_key | translate | values" but this is incorrect. How can I use ng-bind with angular translate interpolation? Is this the correct way to go about rendering a translated value that contains chars such as <, >? Thanks.
*Edit
This almost works but the sanitizer outputs an error.
ng-bind="'locales.user_filter' | translate: { first: user.firstName, last: user.lastName }"
Error: [$sanitize:badparse] The sanitizer was unable to parse the following block of html:
I wound up using a filter before passing the values to the translation key.
.filter('htmlEntities', function () {
return function (input) {
return String(input).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
};
});
var first = $filter('htmlEntities')(scope.user.firstName);
var last = $filter('htmlEntities')(scope.user.lastName);
scope.displayName = $translate.instant('locales.user_filter', { first: first, last: last });
<td ng-bind-html="displayName"></td>

Use dot notation with angular number filter

I have an input field. The user enters a number. I need this number to be formatted correctly with dots representing the thousands (not the decimals)
user input : 5600 -> becomes 5.600 (five thousand six hundred)
user input: 56000 -> becomes 56.000
etc
I need the number to be formatted correctly INSIDE the input field.
I have a fiddle : http://jsfiddle.net/El4a/KPeBD/1059/
This fiddle works perfectly BUT it uses the number filter from angular thus it formats the numbers with a comma-notation. I however need a dot-notation.
So I tried replacing the comma manually by a dot.
var listener = function() {
var value = $element.val().replace(/,/g, '') //delete all notations
var x = $element.val($filter('number')(value, 0)) //filter
$element.val(x).replace(/,/g, '.') //replace comma by dot
}
This didn't work.
I then tried to use a locale cdn (as can be seen in the linked fiddle).
This seemed to be the solution at first creating :
But when the next 0 is added it flips and does :
I figured I also had to change the following line
var value = $element.val().replace(/,/g, '')
to
var value = $element.val().replace(/./g, '')
but then the input field is completely unusable.
Hope someone has a solution for me!
I've removed the directive as it's not that much helpful on filtering the number.
we can use angular filter ($filter) for the same. changed the code into
myApp.filter('number', ['$filter', function ($filter) {
return function (input, symbol, fraction) {
if (input) {
var decimal = input.toString().split('.')[1],
value = input.toString().split('.')[0],
filtered = value.replace(/(?=(\d\d\d)$)/g, ".");
return (decimal) ? (fraction > 0 ? filtered + '.' + decimal.slice(0, fraction) : filtered) : filtered;
} else {
return 0
}
}
}])
sorry I've removed the entire directive section and updated the jsFiddle.
Here is the updated Demo
OMG I finally found it...
I forgot to escape the goddamn . char:
so changing
var value = $element.val().replace(/./g, '')
to
var value = $element.val().replace(/\./g, '')
made it work
48hours well spent...

Compiling a String in Angular Before Displaying

I have the following html:
<div ng-repeat="string in myStrings">
<p>{{string}}</p>
</div>
And a string like this that gets added to $scope.myStrings:
$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: {{2+2}}';
I would like the string to show 4 instead of the {{2+2}} angular expression.
Am I barking up the wrong tree here by trying to do this via $compile? If not, how is it done? Just putting it in compile fails. Do I absolutely HAVE to do this in a directive?
PLNKR FOR REFERENCE
Not sure what your exact goal is, but I can think of two approaches to accomplish this without compiling:
1) Split up the values like so:
<div ng-repeat="string in myStrings">
<p>{{string}}{{mathValue}}</p>
</div>
in controller:
$scope.mathValue = 2+2;
2) Use a function to return the string (I like using this anytime I'm doing anything binding that is non-trivial):
<div ng-repeat="string in myStrings">
<p>{{stringFunction()}}</p>
</div>
in controller:
$scope.mathValue = 2+2;
$scope.stringFunction = function() {
return 'I want to count to 4 via angular: '+$scope.mathValue;
};
I'm not 100% sure whether you are just wanting to count the number of strings in the myStrings array, or just have the ability to add a count, but given your Plunker, you could do the following:
To simply add two variables, update the following line:
$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' + (2+2);
If you wanted to show the count of the number of strings, swap the order of your scope variable declarations and show the myStrings length
$scope.myStrings = ['I am a string', 'I am another string', 'more strings!'];
$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' + $scope.myStrings.length;
Counting the strings will only give you 3, of course, because there are only 3 strings in the array.
Does that solve it for you?
UPDATE
OK - So I think what you want is the count in the string with an ng-click to correspond to the count correct?
If so, then the following on your ng-repeat would do it...
<p>{{string}} {{$index}} </p>
Using $index gives you the index of the repeating item. You can always add 1 to the $index to make it 1-based instead of zero based:
<p>{{string}} {{$index + 1}} </p>
You can append the angular expresion {{}} to the string like:
$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' + {{stuff or 2 + 2}};
Or use $compile Fiddle example
I really needed to use a directive with $compile like shown here:
app.directive('dynamicAlert', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamicAlert, function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
});
http://plnkr.co/edit/bJPEyfkKsertulTN7rWp?p=preview

How to Prefix an angular value in html?

How to prefix an angular value in HTML side.
In my project i am using the below line to show zip code .
<span>{{activeProperty.Zip}}</span>
I want that while displaying the zip, it will check whether the activeProperty.Zip length is 5 or not.if it is less then five then add leading zeros to activeProperty.Zip
For example if 4567 then it will show 04567 but if 45670 is there then will show 46670 only.
I dont want to do this in angular side as updating the activeProperty.Zip in controller leads so many issue as using the same in so many places. Is their any way to do it in HTML side ?
You need to create a filter that will pad the zip with zeroes, see this fiddle:
http://jsfiddle.net/HB7LU/11739/
myApp.filter('pad', function () {
return function(zip) {
var newZip = zip;
if (zip.length < 5) {
newZip = Array(6 - zip.length).join('0') + newZip;
}
return newZip;
};
});
in your html: {{activeProperty.Zip | pad}}
You can still do it angular side without changing model with use of filters, here is simple example that you can extend for your use
app.filter('prefixMe', function() {
return function(input) {
return input.length < 5 ? '0' + input : input
};
});
<span>{{activeProperty.Zip | prefixMe}}</span>

How can I remove all string spaces in AngularJS binding?

I try to do this:
<div id="{{mystring.replace(/[\s]/g, \'\')}}"></div>
but its not working. "mystring" is an object on $scope with string like "my string is this" with spaces I want to remove from the view.
Just create a dedicated filter :
angular.module('filters.stringUtils', [])
.filter('removeSpaces', [function() {
return function(string) {
if (!angular.isString(string)) {
return string;
}
return string.replace(/[\s]/g, '');
};
}])
and call it like :
<div id="{{'hi there'| removeSpaces}}"></div>
If you simply need it in one or two places it may be easier to split and join:
$scope.boundString = 'this is a string with spaces'
with that you could do in your template:
<span>my string is: {{ boundString.split(' ').join('') }}</span>
and you would get:
my string is: thisisastringwithoutspaces
another approach that has been mentioned is the regex version ('g' is for global):
<span>my string is: {{ boundString.replace(/ /g, '') }}</span>
I guess the point is that you can do whatever you want to a string within an expression. These examples are bad convention with respect to Angular dirty-checking. In Angular, bound functions (string.replace, string.split) get evaluated differently opposed to a specified value (string, boolean) when bound to a template's expression. The result of a bound function must be evaluated before Angular knows whether or not to update the DOM. This can be costly over a large app. I would suggest using another variable to track the un-spaced value:
$scope.noSpaces = $scope.boundString.replace(/ /g, '');
HTML:
<span>{{ noSpaces }}</span>
This way, when a digest loop is triggered, Angular will check if noSpaces has changed as opposed to evaluating boundString.replace(/ /g, '').
What if you are ng-repeating? Good question.
for (var idx = 0, idx < $scope.boundIterable.length, i++) {
$scope.boundIterable[i].noSpaces = $scope.boundIterable[i].boundString.replace(/ /g, '');
}
HTML:
<ul ng-repeat="iterable in boundIterable">
<li>{{ iterable.noSpaces }}</li>
</ul>
The directive mentioned works pretty well. But if you want to remove spaces for smaller texts, you can use
.split(" ").join("")
This replaces the complete spaces unlike .replace(" ","") which replaces only the first space.
You can replace all spaces by blank by using replace():
.replace(" ","")
How about {{ string.trim() }}?
Source
You can do it by using replace():
{{mystring.replace(" ","")}}
that's it I hope so.
removeSpaces() {
originalText ="hi! here i'm";
removedSpacesText = originalText.split(" ").join("");
}

Resources