I have a problem adding a number from a textbox and place it on another textbox. My situation is like this:
I have one textbox:
<input type="text" ng-model="num1" />
And whatever the input is on this textbox, this will be the value of another textbox like this:
<input type="text" value="{{ num1 + 1 }}" />
This setup shows successfully but with errors. When I type 2013 on the first, the second shows 20131 instead of 2014.
I also tried using filters like this:
<input type="text" value="{{ num1 + 1 | number}}" />
but unfortunately, it doesn't work. What did I miss?
The value of a textbox is a string.
You need to convert it to an integer/float you can do that using parseInt or parseFloat if you have decimals. By default you don't have the parseInt method available in your expression, but you can add it to the scope in your controller to make it available:
$scope.parseInt = parseInt;
<input type="text" value="{{ parseInt(num1) + 1 }}" />
You can see this in action here
You can also use input type=number instead of text. This also has the added benefit of providing a type that matches what you expect to get.
num1 is being interpreted as a string.
Use parseInt() or parseDouble() to convert this to a number when initializing it.
I found a tick method to solve. My case is that I need to parseInt something like item.qty. But I found <div ng-click="item.qty = parseInt(item.qty) + 1>Add</div> won't work.
Finally, I found this method can solve
<div ng-click = "item.qty = item.qty - 1 + 2">Add</div>
Related
We have an address form and would like to set a default value of x to the input phone.
<div
class="form-group ms-mb-xs"
data-ng-if="field.settings.display_type=='phone'"
>
<label>
{{field.label|msTranslate}}
</label>
<input
type="tel"
id="phone"
class="form-control"
data-ng-model="fields[field.name]"
data-ng-intl-tel-input
data-ng-init="fields[phone]=12345678"
/>
</div>
I have used data-ng-init and ng-init and tried putting a default value but it doesn't show on the front-end. Also tried value="12345678" but didn't work. Have also put the numbers in single quotes but still didn't work.
Thanks in advance.
There are two ways you can do this.
Initialize your model variable with the value you need to show.
Use ng-init to execute code so that your model variable will be given a value.
It looks that you have tried the second option data-ng-init="fields[phone]=12345678"
The only reason why the option 2 doesn't work is that the controller updates the fields[field.name] with a blank value.
So, make sure you give a default value to the model variable if the intended value is blank. Something like this:
fields[field.name] = fromDB || 'a default value'
I have an array like this:
var data = [{
"chamberName": "Community App",
"representativeEmail": "some#email.com",
"primaryColor": "#325490",
}]
And an input on a form like this:
<input type="text" class="form-control" id="chamberName" ng-change="" placeholder="Chamber Name">
I'd like to use ng-change to change the array value that matches the changing input value. I can't figure out how to get the input value though. I am thinking something like:
ng-change="data[0].chamberName = inputValue";
Any help would be great!
SIDE NOTE: You need to use $parent when using ng-include.
You need to use ngModel.
Why your original data in array?
If you need to edit only one instance it is simpler to use object and pass object's property to ngModel
<input .. data-ng-model="data.chamberName" .. />
Or if you are going to edit pool of objects, than you can surround it with ngRepeat
<fieldset data-ng-repeat="elm in data">
<input .. data-ng-model="elm.chamberName" .. />
..
</fieldset>
Or if you still want to store your single object in array
<input .. data-ng-model="data[0].chamberName" .. />
I use a bootstrap modal for a form in my code.
So, I need standard-values for two textfields, and I tried to simply use VALUE like
<input type="text" value="myStandardText" ng-model="something">
I think it's because of the ng-model, but why?
And do you know a way to get the same result as with value, too?
(For now, I use "placeholder" instead, but value would be better)
UPDATE: Ok, i got a step closer.
<input type="text" ng-model="e_uname" ng-init="e_uname = 'a std txt'">
does the trick, but i need now to set a saved value as init-value.
i added "uname" to my scope, but with input it doesn't work.
<input type="text" ng-model="e_uname" ng-init="e_uname = {{uname}}">
I tried also with different quote marks. Any ideas?
Ok guys, I found the answer.
Final solution is:
<input type="text" ng-model="e_uname" ng-value="uname">
I'm trying to solve an issue with formatting some input fields.
When I enter the value I want the model to updated as I type, like this:
I enter ex '1200000', i want it to display in the input field '1.200.000' and the model should update as 1200000.
I also want the field to pre-formatted if the model already has a value.
Likewise I need to format in percentage, please see my example:
<div ng-controller="testController as testCtrl">
{{ 'Model: ' + testCtrl.value1 }}
<input type="text" cr-numeric ng-model="testCtrl.value1" /> <br />
{{ 'Model: ' + testCtrl.value2 }}
<input type="text" cr-numeric="interest_3_decimals" ng-model="testCtrl.value2" />
The formatting work fine when I'm entering the values, but I can't make it to work when the model already has a value and to update to value accordingly when i'm entering data.
Please anyone help.
If You need format only in view, You can use number filter;
Just:
Model: {{ testCtrl.value1 | number:2 }}
So, in model You still have original value;
Sorry, english is my second language.
Here is my problem. I had bound two textboxes to the scope. Each textbox needs to affect the other on changes. You'll understand that it cause conflict. From now, I've got that :
<input type="text" ng-model="celcius" ng-change="farenheit=celcius * 9/5 + 32 || ''">
<input type="text" ng-model="farenheit" ng-change="celcius=(farenheit - 32) * 5/9 || ''">
But it doesn't work properly. Any idea how to trick that ?
You need to use type="number" instead of type="text" so the expression in the ng-change directive can calculate the values as numbers instead of text.