<input type="text" ng-model="user.User.DateOfBirth"> is in my view. user.User.DateOfBirth is currently in YYYYMMDD format, but I want it to show as mm/dd/yyyy format in the view. Is it possible to convert this just for the display?
This also may help you
http://jsfiddle.net/mikeeconroy/Hu9m2/1/
.filter('myFormatDateFilter',function(){
return function(input){
if(angular.isDefined(input)){
if(input.length >= 8){
input = input.slice(0,8);
input = input.slice(4,6) + '/' + input.slice(6,8) + '/' + input.slice(0,4);
}
}
return input;
};
});
For outputting the data in the correct format try filters (in your case the date filter: http://docs.angularjs.org/api/ng.filter:date)
Well, for inputs that's another case. I guess you could always bind that input to another property, watch that and parse it yourself and then assign it to your real model...
Some more insight, you could try something like this:
HTML:
<input type="text" ng-model="myDateInput">
In your JS controller use the following:
$scope.$watch('myDateInput', function (newValue) {
$scope.user.User.DateOfBirth = $filter('date')(newValue, 'yyyy-MM-dd'); // Or whatever format your real model should use
});
$scope.$watch('user.User.DateOfBirth', function (newValue) {
$scope.myDateInput = $filter('date')(newValue, 'yyyy-MM-dd'); // Or whatever format your input should use
});
It would probably be better though to force the user to use a certain input format, either by some sort of pattern matching (and therefore validation) or by some sort of special date input/widget.
Related
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...
I am using xeditable to edit data inline. Now I want to edit a numeric field, but the source data contains the number as string formatted. So I get the error ngModel:numFmt. How can I convert a string formatted number (example: 0.3) into a numeric for xeditable?
I tried with the string-to-number directive, but it seems, that xeditable did not recognize it:
<td>
<span data-editable-number="row.factor"
data-ng-model="row.factor"
data-e-name="factor"
onbeforesave="validateRow(data, row.id)"
onaftersave="save(row)"
data-e-required
string-to-number="string-to-number"
>
{{row.factor}}
</span>
</td>
You can create a filter to convert String to Integer
app.filter('num', function(){
return function(input) {
return parseInt(input, 10);
};
});
and use it like this
editable-number="row.factor | num "
Working Plunker
The previous didn't work updating the model, better way to achieve this is to transform the data beforehand.
e.g
var rows = [{factor:"1"}, {factor: "4"}, {factor: "9"}];
var tranformed_rows = rows.map(function(row) {
row.factor = parseInt(row.factor);
return row;
});
and then use tranformed_rows in your view.
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>
hi i'm really struggling with this and not sure how to proceed as I haven't found anything ont his. Here's the plunkr example I'm working with: example.
I want to append something at the end of the results for example if I type S, I want to get the results and the last line should say "Look for S in other shops" :
Sauron
Bowser
Yoshi
Look for S in other shops
As you can see in my plunker, whatever I do in the template, ends up getting repeated.
Define your custom filter:
.filter('finalAppend', function(){
return function(array, value){
array.push({
name: 'Look for ' + value + ' in other shops',
type: 'good'
});
return array;
}
});
And use it like this:
<input type="text" ng-model="selected"
typeahead="datum.name for datum in (data|filter:$viewValue|limitTo:8)|finalAppend:$viewValue"
typeahead-template-url='tpl.html'>
Example
In e2e testing, I want to simulate user typing in a input field. My input field has a maxlength in it. I want the maxlength to be consider, but I have not succeed to find a solution to it. Here's what I've tried:
<input type="text" id="myField" maxlength="10" ng-model="myModel" />
<span>{{myModel}}</span>
1) input("myModel").enter("ABCDEFGHIJKLMNOPQRSTUVWXYZ"). Both the myField input and the myModel model are set to "ABCDEFGHIJKLMNOPQRSTUVWXYZ". Expected result: "ABCDEFGHIJ"
2) element("#myField").val("ABCDEFGHIJKLMNOPQRSTUVWXYZ"). The myField input is set to "ABCDEFGHIJ". Which is perfect! But the myModel is null.
Do you guys have a solution for this? I don't want to use ngMaxlength since I want to prevent to use to typing more than 10 caracters.
Many thanks!
You could extend angular-scenario dsl and use a function similar to this one to inject a value in ngModelController
function typeIn(element, str) {
var maxlength = parseInt(element.attr('maxlength'), 10);
var value = element.val() + str;
if (maxlength !== NaN) {
value = value.substr(0, maxlength);
}
element.val(value);
element.controller('ngModel').$setViewValue(value);
element.scope().$apply();
}
http://jsfiddle.net/nxNm9/1/