Angularjs ng-value sum fields - angularjs

Hi i have inputs like this
<input type="text" ng-model="tbl.Public">
<input type="text" ng-model="tbl.Private">
<input type="text" ng-value="tbl.Public--tbl.Private" ng-model="tbl.Total">
the above form will working fine it will sum the Public and Private value and put it in tbl.Total field. My problem is in edit form where value of tbl.Total, tbl.Public, tbl.Private are assign from database.
js
$scope.tbl.Public=10;
$scope.tbl.Private=25;
$scope.tbl.Total=35;
now after assigning a value from js when i change value of tbl.Public or tbl.Private in form it is not affecting a tbl.Total it should sum the two value and put it in tbl.Total field.
Thank you for your any help and suggestion.

ng-value is usually used on radiobuttons and option elements, it's not a good fit for your use case.
A better thing to do would be implementing an updateTotal() function combined with ng-change. I would also recommend changing your input types to number so you're not allowing users to sum text.
<input type="number" ng-model="tbl.Public" ng-change="updateTotal();">
<input type="number" ng-model="tbl.Private" ng-change="updateTotal();">
<input type="number" ng-model="tbl.Total">
In your controller:
$scope.updateTotal = function() {
$scope.tbl.Total = $scope.tbl.Public + $scope.tbl.Private;
}

It should be like this to prevent from concatenate
$scope.updateTotal = function() {
var Public = Number($scope.tbl.Public || 0);
var Private = Number($scope.tbl.Private || 0);
$scope.tbl.Total = Public + Private;
}

Related

ng-model - getting name and value of input box

I have an update function and a number of input boxes. Each input has an ng-blur attached to it so that the update function is called whenever the cursor leaves the box.
$scope.update = function(data) {
console.log(data); //outputs value in the textbox
//how can I output/access the key?
}
The input for name look like this:
<input type="text" ng-model="user.name" ng-blur="update(user.name)"/>
As I need to be able to post a JSON object in the form {"name" : "bob smith"} what's a good way of generating the "key" of the object bearing in mind that it will differ depending on the input box that's being used at the time?
EDIT ↓
I have made this jsfiddle to illustrate a way to do it more cleanly & that would scale more easily: http://jsfiddle.net/kuzyn/k5bh0fq4/5/
EDIT ↑
Why not simply pass a second string argument? It's not a fancy way to do it but it would work:
<input type="text" ng-model="user.name" ng-blur="update(user.name, 'name')"/>
And
$scope.update = function(data, key) {
console.log(key, data);
}
It might be a little more work but it is much more scalable. You could make use of the ng-form and naming your form inputs. By naming your form and inputs, you are creating a form reference on your scope via $scope[form-name]. Each named input within that form then sets an input reference via $scope[form-name][input-name].
I'm coding this in coffeescript (to preserve my sanity, sorry)
form
<form name="myForm">
<input name="name" ng-model="user.name" ng-blur="update(user)"/>
<input name="email" ng-model="user.email" ng-blur="update(user)"/>
<input name="other" ng-model="user.other" ng-blur="update(user)"/>
</form>
update & save func
# key is what changed in case you need to do something special on the put call to server
$scope.save = (data, key)->
# pseudo-code
$scope.update = (data)->
for name, input of $scope.myForm
if input?.$dirty
$scope.save data, name
break
docs - https://docs.angularjs.org/guide/forms
codepen - http://codepen.io/jusopi/pen/LGpVGM?editors=101

ng-list length validation in AngularJS

I need validation for ng-list, means should restrict the user to enter more than three array list like ['121','565','435'] and if user tries to enter like ['121','565','435','787'] should give error like only 3 vin can enter.
Also if the user enter ['**1214**','565','435'] like above it should tell only 3 digits are allowed.
This is my input field:
<input type="text" name="vin" id="vin" class="form-control"
ng-model="vm.user.vin" ng-list required max-length="3"/>
I am new in AngularJS.
I don't know the default validate properties for ng-list, but you can customise the validation like
<input type="text" name="vin" id="vin" class="form-control"
ng-model="vm.user.vin" ng-list required max="3" ng-change="isBigEnough(vm.user.vin)" />
<span ng-show="vm.user.vin.length > 3">array length should below than three</span>
<span ng-show="IsElementLength">element length should below than three</span>
and your controller side code
$scope.isBigEnough = function (values) {
if (values != undefined) {
values.every(function (element, index, array) {
if (element.length > 3) {
$scope.IsElementLength = true;
return false;
}
$scope.IsElementLength = false;
return true;
})
}
}
Result
['121','565','435'] - No error message
['1211','565','435'] -element length should below than three
['121','565','435','543']- array length should below than three
['1211','565','435','543']- array length should below than three and element length should below than three
i have bring the code on plunker, but i did't test it on plunker.

Is there any way I can find out the required length of an input field using AngularJS?

Given this:
<input id="modalContentTitle"
name="modalContentTitle"
ng-minlength="5"
ng-model="ahs.modal.data.title"
ng-required="true" />
I know that I can access information on that field like this:
title="{{ ahs.vr5(ahs.forms.modal.modalContentTitle) }}"></i>
vr5 = function (field) {
if (angular.isDefined(field)) {
if (field.$error.required) return "Required";
if (field.$error.minlength) return "Minimum 5 characters";
if (field.$error.email) return "Email Invalid";
}
return "OK";
}
Is there a way that I can get the ng-minlength directly from the field information with AngularJS or do I need to create a different vr6 function if I want to verify lengths for fields with a minlength of 6.
unfortunately minlength is a private variable within Angular. You can however, do a workaround for this
<input id="modalContentTitle"
name="modalContentTitle"
ng-minlength="modalContentTitle.minlength = 5"
ng-model="ahs.modal.data.title"
ng-required="true" />
And now you can access this by
field.minlength

AngularJS dependant inputs link to the same scope variable

I'm new to angularJS and I'm trying to design a form including 2 input fields which are internally tied to the same scope variable: saying the first field is the speed in km/h and the second field is the speed in m/s. The behavior I'd like to obtain is: whatever field the user changes the other field would be updated.
The problem I'm running into is the following:
When the speed in m/s is changed by the user, the speed in km/h is updated which lead to update again the speed in m/s, which update the speed in km/h until no value change. The side effect is when the speed in m/s is a round number saying 1 and the user change the value to make it decimal entering the decimal dot - the update loop lead to remove the dot because from a calculus point of view 1. is 1.
What is the best way to avoid that?
Here are the fragments of code (html):
<label for="SpeedKm">Speed (Km/h):</label>
<input id="SpeedKm" name="SpeedKm" type="text"
placeholder="km/h"
ng-model="speedKmh"
ng-model-options="{ getterSetter: true}">
>
<label for="SpeedMs">Speed (m/s):</label>
<input id="SpeedMs" name="SpeedMs" type="text"
placeholder="m/s"
ng-model="speedMs"
ng-model-options="{ getterSetter: true}">
>
and the controller
angular.module('speedconverter')
.controller('MainCtrl', ['$scope', function ($scope) {
$scope.speed = 10; //km/h
$scope.speedKmh = function(newValue) {
if (angular.isDefined(newValue)) {
$scope.speed = newValue;
}
return $scope.speed;
};
$scope.speedms = function(newValue) {
if (angular.isDefined(newValue)) {
$scope.speed = newValue*3.6;
}
return $scope.speed/3.6;
};
});
Thank you

Calling an angularjs function during controller init

I have a large amount of code that I inherited. It is working according to the original spec, so although it may or may not be the "right" way of doing things, it does work and I'd prefer not to mess with it. (I am VERY new to angular, and under a tight deadline - I don't have time right now to "fix" things that aren't broken, even if they're not being done the "correct" way.)
I have a checkbox and a group of address fields. When the checkbox is checked, a function runs that populates the address fields.
<input id='as_check' type="checkbox" ng-model='model.prepop_addr' ng-change="model.handlePrepopAddress()">
<input type="text" placeholder="Address 1" required ng-model='model.payment.addr1'/>
<input type="text" placeholder="Address 2" ng-model='model.payment.addr2'/>
<input type="text" placeholder="City" required ng-model='model.payment.city'/>
<input type="text" placeholder="State" required ng-model='model.payment.state'/>
<input type="text" placeholder="Zip" required ng-model='model.payment.zip'/>
In my controller, I have a function called handlePrepopAddress():
model.handlePrepopAddress = function () {
if (model.prepop_addr) {
console.log("prepop is true");
var del_addr = $window.user_state.deliveryAddress;
model.payment.addr1 = del_addr.address;
model.payment.addr2 = null;
model.payment.city = del_addr.city;
model.payment.state = del_addr.state;
model.payment.zip = del_addr.zip;
} else {
console.log("prepop is false");
model.payment.addr1 = null;
model.payment.addr2 = null;
model.payment.city = null;
model.payment.state = null;
if (!model.payment.saved) {
model.payment.zip = null;
}
}
return true;
};
When I click the checkbox, this runs correctly and updates the various fields. Requirements have changed, and the client now wants the checkbox defaulted to checked, which would then default the values of the fields. Rather than duplicating code, I thought I'd be able to just run the function at the start of the controller, like this:
place_order.controller('AsCreditCardCtrl', ['$scope', 'PaymentModel', function ($scope, model) {
$scope.model = model;
model.prepop_addr = true;
model.handlePrepopAddress();
}]);
The function is running, as the console.log statements I have in the function are running. But the view is not updating; the checkbox is visibly unchecked and the fields are empty aside from the initial placeholder values.
What am I missing? How can I make the function update the view?
You can try
var i= model.handlePrepopAddress();
It might work.

Resources