After reset, angular retrieves values from previous input - angularjs

This is my app in Angular JS:
http://cmpe-273-010825867-fall-2016.herokuapp.com/
The problem is, when I input values into the two texts by clicking the numeric buttons, they are entered. When, i reset, the values disappear.
But when I reenter the values using the numeric buttons again, the values are appended to the values before the reset.
Here is my angular code:
var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope) {
function resetFields(){
$scope.opReset=' ';
}
function add(x, y) {
return x + y;
}
function sub(x, y) {
return x - y;
}
function mul(x, y) {
return x * y;
}
function div(x, y) {
return x / y;
}
function calc(op, x, y) {
return $scope.operators[op](parseInt(x, 10), parseInt(y));
}
$scope.operators = {
'+': add,
'-': sub,
'*': mul,
'/': div
};
$scope.op = '+';
$scope.calc = calc;
});
My HTML:
<div ng-app="myApp">
<div ng-controller="myController" ng-init='isFocused=true'>
<form>
<div class="col-lg-5 col-md-8 col-sm-12 col-xs-12">
<input width="100%" type="text" class="form-control" id="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' value="{{opReset}}" autofocus>
</div>
<div class="col-lg-2 col-md-8 col-sm-12 col-xs-12 text-center" style="font-size:32px">
{{op}}
</div>
<div class="col-lg-5 col-md-8 col-sm-12 col-xs-12">
<input width="100%" type="text" class="form-control" style="height:50px; font-size:32px" ng-model="operand2" ng-focus=' isFocused="operand2" ' id="operand2" value="{{opReset}}">
</div>
<script src="js/custom.js"></script>
</div>
</div>
<div class="col-lg-5">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12 eqProps text-center">
=
</div>
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12"style="font-size:32px">
{{output}}
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-1"></div>
<div class="col-lg-7">
<div class="row">
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></div>
<div class="col-lg-10 col-md-10 col-sm-10 col-xs-10">
<div class="row-fluid">
<button class="btn btn-danger btn-fonts" type="reset" value="reset" ng-click=" opReset=resetFields() ">C</button>
<div class="btn btn-default btn-fonts" ng-repeat="n in [0]" style="margin-left:2px" ng-click='$parent[isFocused]=$parent[isFocused]+""+n' ng-disabled='isFocused===true'>{{n}}</div>
<div class="btn btn-default btn-fonts" ng-click="output=calc(op,operand1,operand2)" style="visibility:hidden">=</div>
<div class="btn btn-primary btn-fonts" ng-click="output=calc(op,operand1,operand2)">=</div>
</form>

Why are you using value="operand1" at all?
<input width="100%" type="text" class="form-control" id="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' value="{{opReset}}" autofocus>
You can directly use ng-model to initialize the values, as well as clear it on clicking C button.
Change your text area code to remove values.
<input width="100%" type="text" class="form-control" id="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' autofocus>
And then in resetFields function, change the values using:
$scope.operand1='';
$scope.operand2='';

please make empty value for the ng-model values(operand2,operand1)

Related

Angularjs directive doesn't popup with individual selected row

I am trying to pass each scope.selectedLiability based on selection into popup modal from recordRecommendedLiabilityCash directive into detailViewRecommendedLiabilityCash directive on each click.
Here are the core function in recordRecommendedLiabilityCash.
scope.openDetailView = function (id) {
// console.log(scope.recommendedLiabilities[id]);
scope.selectedLiability = scope.recommendedLiabilities[id];
console.log(scope.selectedLiability);
if (!scope.detailViewFlag) {
scope.detailViewFlag = true;
}
};
Up to here everything is okay. In each click console.log(scope.selectedLiability); shows respective selected data. Now, I am trying to pass scope.selectedLiability into nested directive detailViewRecommendedLiabilityCash. However, only first or 0 index value popups in every click.
Picture for index 0
Picture for index 2
If you look carefully on images, we can see both data are same in both clicks. However, I need respective selected data in corresponding click.
Sorry for messed up code. This is not my own code. I am trying help my friend. I stuck here.
//recordRecommendedLiabilityCash directive
.directive('recordRecommendedLiabilityCash', function () {
return {
restrict: 'E',
templateUrl: 'cash/savings-liabilities/recommended-liability-accounts/record-recommended-liability.html',
scope: {
rlc: '=',
liabilities: '=',
listOfLumpSumSource: '=listOfLumpSumSource',
id: '=',
recommendedLiabilities: '='
},
link: function (scope, element, attrs) {
scope.detailViewFlag = false;
scope.removeRecommendedLiabilityRecordCash = function () {
if (scope.rlc.active) {
scope.rlc.active = false;
}
// To-Do: Refactor into reusable function
for (var i = 0; i < scope.liabilities.length; i++) {
if (scope.rlc.legacyId === scope.liabilities[i].id) {
if (!scope.liabilities[i].active) {
scope.liabilities[i].active = true;
}
}
}
};
scope.openDetailView = function (id) {
// console.log(scope.recommendedLiabilities[id]);
scope.selectedLiability = scope.recommendedLiabilities[id];
console.log(scope.selectedLiability);
if (!scope.detailViewFlag) {
scope.detailViewFlag = true;
}
};
}
};
})
//detailViewRecommendedLiabilityCash directive
.directive('detailViewRecommendedLiabilityCash', function () {
return {
restrict: 'E',
templateUrl: 'cash/savings-liabilities/recommended-liability-accounts/detail-view-recommended-liability.html',
scope: {
rlc: '=',
listOfLumpSumSource: '=listOfLumpSumSource',
detailViewFlag: '=',
recommendedLiabilities: '=',
id: '=id',
selectedLiability: '=selectedLiability'
},
link: function (scope, element, attrs) {
console.log(scope.selectedLiability);
scope.rlc.payment_amount = '';
scope.rlc.due_amount = scope.rlc.remainingBalance;
console.log(scope.listOfLumpSumSource);
scope.selectedLumpSumSource = null;
console.log(scope.selectedLumpSumSource);
console.log(scope.recommendedLiabilities, scope.id);
scope.lumpSumDataSource = [
{
"companyName": "FCB",
"productName": "Product1"
},
{
"companyName": "First Command",
"productName": "Product2"
},
{
"companyName": "Second Command",
"productName": "Product3"
}
];
//scope.lumpSumSource = scope.rlc.lumpSumSource;
console.log(scope.rlc.lumpSumSource);
//scope.lumpSumDataSource.push(scope.rlc.lumpSumSource);
// Intialize default variables
var defaultLumpSumObject = {'lumpSumSource': '', 'lumpSumAmount': '', 'remainingBalance': ''};
scope.lumpSumArray = [defaultLumpSumObject];
scope.exisitingAccountObj = {
monthlyPayment: scope.rlc.monthlyPayment
};
// Function to add lumpsum source
scope.addSource = function() {
scope.lumpSumArray.push(defaultLumpSumObject);
};
// Function to delete lumpsum source
scope.deleteSource = function(index) {
scope.lumpSumArray.splice(index,1);
};
scope.closeDetailView = function () {
if (scope.detailViewFlag) {
scope.detailViewFlag = false;
}
};
// When the user clicks anywhere outside of the modal, close it
element.on('click', function (e) {
var target = $(e.target);
if (!target.closest('.modal-content').length) {
scope.$evalAsync(scope.closeDetailView());
}
});
scope.saveRecommendedLiabilityAccount = function () {
scope.rlc.remainingBalance = scope.rlc.remainingBalance - scope.rlc.payment_amount;
scope.rlc.lumpSumAmount = scope.rlc.lumpSumAmount - scope.rlc.payment_amount;
console.log(scope.rlc.lumpSumAmount);
};
}
};
});
<!--recordRecommendedLiabilityCash template -->
<div class="Table-row">
<div class="Table-row-item" data-header="Company">
<a class="edit-view" ng-click="openDetailView(id)" data-toggle="modal" data-target="#{{'modal-detail-view-cash-rec-liability-' + rlc.id}}">{{ rlc.companyName }}</a>
</div>
<div>{{selectedLiability}}</div>
<div class="Table-row-item" data-header="Product">{{ rlc.productName }}{{rlc.id}}{{id}}</div>
<div class="Table-row-item" data-header="Liability Type">{{ rlc.liabilityType }}</div>
<div class="Table-row-item" data-header="Owner">{{ rlc.owner }}</div>
<div class="Table-row-item" data-header="Loan Balance">{{ rlc.remainingBalance | currency:"$":0 }}</div>
<!-- <div class="Table-row-item" data-header="Lump Sum">{{ rlc.LibtyLumpSumFundBal | currency:"$":0 }}</div>-->
<div class="Table-row-item" data-header="Monthly">{{ rlc.monthlyPayment | currency:"$":2 }}</div>
<div class="Table-row-item custom-select" data-header="Action">
<select id="action" name="action">
<option value="">I Want To …</option>
<option value="Retain">Retain</option>
<option value="Consolidate">Consolidate</option>
<option value="Pay-Off">Pay-Off</option>
<option value="Pay-Down">Pay-Down</option>
</select>
<div class="select__arrow"></div>
</div>
<!-- <div class="Table-row-item status" data-header="Status">
<div>{{ rlc.status }}</div>
<a class="close-button" ng-click="removeRecommendedLiabilityRecordCash()"></a>
</div>-->
</div>
<detail-view-recommended-liability-cash rlc="rlc" selected-liability="selectedLiability" recommended-liabilities="recommendedLiabilities" id="id" list-of-lump-sum-source="listOfLumpSumSource" detail-view-flag="detailViewFlag"></detail-view-recommended-liability-cash>
<!-- template for details view recommended liability-cash -->
<div class="modal fade" role="dialog" id="{{'modal-detail-view-cash-rec-liability-' + rlc.id}}">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<span class="close" data-dismiss="modal">×</span>
<h4>Savings - Liabilities <label class="pull-right">{{ rlc.remainingBalance }}</label></h4>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<label class="header-sub-label">Owner: <span>{{ rlc.owner }}</span></label>
<label class="header-sub-label pull-right">Loan Balance</label>
</div>
</div>
</div>
<form name="recommendedLiabilityForm" novalidate >
<div class="modal-body">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Company</label>
<label class="control-value">{{ selectedLiability.companyName }}</label>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Product</label>
<label class="control-value">{{ selectedLiability.productName }}</label>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<label class="control-label">Description</label>
<label class="control-value">{{ selectedLiability.description }}</label>
</div>
</div>
<div class="row display-row">
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Liability Type</label>
<label class="control-value">{{ selectedLiability.liabilityType }}</label>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Mortgage Type</label>
<label class="control-value">{{ selectedLiability.mortgageType }}</label>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Interest Rate</label>
<label class="control-value">{{ selectedLiability.interestRate | currency:"$":2 }}%</label>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Payment Type</label>
<label class="control-value">{{ selectedLiability.paymentType }}</label>
</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Current Monthly Payment</label>
<label class="control-value">{{ selectedLiability.monthlyPayment }}</label>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<label class="control-label">Payment Frequency</label>
<label class="control-value">{{ selectedLiability.paymentFrequency }}</label>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<label class="control-label">Remaining Term</label><br>
<label class="control-label">Years</label><span> {{ rlc.remainingTermYears }} </span>
<label class="control-label">Months</label><span> {{ rlc.remainingTermMonths }} </span><br>
<label class="control-label">End Date:</label><span> {{ rlc.remainingTermEndDate | date:'yyyy-MM-dd' }} </span>
</div>
</div>
<hr />
<label class="heading">Funding</label>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 form-group">
<label for="monthlyFunding">Recommended Monthly Payment</label>
<input name="monthlyFunding" ng-model="exisitingAccountObj.monthlyPayment" class="form-control col-md-6 col-lg-6 col-sm-6" type="text" />
</div>
</div>
<label class="heading">Lump Sum</label>
<div class="row" ng-repeat="lumpSumData in lumpSumArray track by $index">
<div class="col-lg-3 col-md-3 col-sm-3 form-group">
<label for="source" class="control-label">Source</label>
<select name="source" ng-model="selectedLumpSumSource"
ng-options="lumpSumSource as lumpSumSource for lumpSumSource in listOfLumpSumSource"
class="form-control" ng-change="getLumpSumAmount(selectedLumpSumSource)"></select>
<!--<select name="source" ng-model="source" ng-options="source for source in lumpSumSource" class="form-control"></select>-->
</div>
<div class="col-lg-3 col-md-3 col-sm-3 form-group">
<label for="amount" class="control-label">Amount</label>
<input name="amount" ng-model="rlc.payment_amount" class="form-control" type="number" min="0" max="{{rlc.remainingBalance}}" required />
</div>
<div ng-messages="recommendedLiabilityForm.amount.$error" style="color:red">
<div ng-message="validationError">this is the error</div>
<div ng-message="required">Must have value</div>
<div ng-message="minlength">Must be 5 char length</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4">
<label class="control-label">Remaining Balance</label>
<label class="control-value">{{selectedLiability.lumpSumAmount - selectedLiability.payment_amount | currency:"$":2}}</label>
</div>
<div class="col-lg-2 col-md-2 col-sm-2 vertical-line">
<span class="glyphicon glyphicon-trash" ng-click="deleteSource($index)"></span>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 add-button-container">
<button type="button" class="btn btn-primary" ng-click="addSource()"><span class="glyphicon glyphicon-plus"></span><span class="add-more-button">Add Another Source</span></button>
</div>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">cancel</button>
<button type="submit" class="btn btn-primary" ng-click="saveRecommendedLiabilityAccount()">Save</button>
</div>
</div>
</div>
</div>
It is not clear with the code you pasted, but looks like you are creating N modals, one per each row:
<a class="edit-view" ng-click="openDetailView(id)" data-toggle="modal" data-target="#{{'modal-detail-view-cash-rec-liability-' + rlc.id}}">{{ rlc.companyName }}</a>
This link should open the modal that matches the same rc.id. This means you have N <detail-view-recommended-liability-cash/> directives, one per rc.id. Isn't it? If you did not generate the same amount of <detail-view-recommended-liability-cash/>, one per row, then you cannot identify the opening modal. Still, I think this is a bad idea, as unfortunately you have N modals, all pointing to the same scope.selectedLiability object. I'm not 100% sure, but if you mess something, can be the case that you're always seeing the last rendered modal. Also, bootstrap is plain JS + jQuery, and you're using Angular for the directive.
I think what is happening is that when you click for the first time, Angular is rendering all the modals pointing to the same object, but on the subsequent clicks the objects are not refreshed on the directives. Perhaps a $scope.$apply() can fix the bug?
Try to clean your code so you have only one modal, and make sure it is isolated as a directive, and before doing scope.detailViewFlag = true, make sure you cleaned everything before. In general, JS and Angular do not play well, and you need to abuse the use of scope.$apply() on the JS calls. That's why the angular directives for bootstrap isolates the modals passing a new object, a new controller, etc.
To sum up: you have a lot of modals on your code, one per row, pointing to the same object on the recordRecommendedLiabilityCash directive. When you click for the first time, all the modals are "filled", but on the second click you never cleaned the information from the modals, and Angular never refreshed the information. To fix this: use only one modal, and make sure you're cleaning the information before opening the modal.
90% sure that's your problem. Try to avoid modals by id, and control them manually on JS within Angular, and check when to execute the $apply().
Regards!

how to validate two select boxes in angular js

this is my html form that have to use to get the details but when i use the required attribute in the select tag, only the first one is validated and not the second one or an other field after the select tag, for eg if i select option in the first select and click post project then there is no error about the validation, and if i dont select the data from the first select tag there is validation error popup saying that the field is required,
<form name="myForm" role="form" action="#">
<div class="well" style="margin-left:20px; margin-right:20px">
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-lg-12">
<h4 style="color:darkblue">What type of project do you require?</h4>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-6 col-lg-6">
<select name="drop1" class="form-control" ng-model="project.match1">
<option value="">-- Select Category --</option>
<option ng-repeat="items in serviceCategory" value="{{items.ServiceCategoryId}}">{{items.ServiceCategoryName}}</option>
</select>
</div>
<div class="col-md-6 col-lg-6">
<select class="form-control" ng-model="project.match2" name="drop2">
<option value="">-- Select Sub Category --</option>
<option ng-repeat="a in service" ng-if="a.ServiceCategoryId==project.match1">
{{a.ServiceName}}
</option>
</select>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<textarea ng-model="project.ServiceCategories" placeholder="Add Service Categories here" rows="3" style="resize:none; width:100%"></textarea>
</div>
</div>
</div>
<div class="well" style="margin-left:20px; margin-right:20px">
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12 text-left">
<h4 style="color:darkblue">What is your Project about</h4>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12 text-left">
Describe your Project<br>
</div>
</div>
<div class="row" style="padding-top:10px; margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<input type="text" ng-model="project.DescribeProject" style="width:100%">
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-6 col-lg-6">
What is your Project about?
</div>
<div class="col-md-6 col-lg-6">
</div>
</div>
<div class="row" style="padding-top:10px; margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<textarea ng-model="project.AboutProject" ng-trim="false" rows="3" cols="70" ng-maxlength="5000" style="resize:none; width:100%;"></textarea>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
Attach files
</div>
</div>
<div class="row" style="padding-top:10px; margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<!--attach file code-->
<div style="height:30px; width:100%;">
</div>
</div>
</div>
<div class="row" style="margin-bottom:15px; margin-left:5px; margin-right:5px">
<div class="col-md-1 col-lg-1"></div>
<div class="col-md-10 col-lg-10">
<div class="text-right">
<button class="btn btn-default btn-primary" type="submit" ng-click="showData(project)">Post Project</button>
<button class="btn btn-default" data-dismiss="modal" aria-label="Close" style="color:black; border-color: #2e6da4">Close</button>
</div>
</div>
<div class="col-md-1 col-lg-1"></div>
</div>
</div>
</form>
The Script is
var app = angular.module("MyApp", []);
app.controller('MainCtrl', function ($scope, $http) {
$http.get('/JsonData/ServiceCategory.json').success(function (response) {
console.log("Service Category Connected");
$scope.serviceCategory = response;
});
$http.get('/JsonData/Service.json').success(function (res) {
console.log("Service Connected");
$scope.service = res;
});
$scope.showData = function (inputData) {
var Project = new Object();
Project.Category = inputData.match1;
Project.SubCategory = inputData.match2;
Project.ServiceCategories = inputData.ServiceCategories;
Project.Description = inputData.DescribeProject;
Project.AboutProject = inputData.AboutProject;
var ProjectJson = JSON.stringify(Project);
alert(ProjectJson);
}
});

how to max and min the ngdialog by on click inside of ngdialog popup

this is my ngcontolller code
$scope.openModalContant = function () {
ngDialog.open({
template: 'myModalContent.html',
scope: $scope,
showClose: true,
closeByDocument: false,
closeByEscape: false,
preCloseCallback: function(value) {
$scope.clearFields($scope.primaryObject)
return true;
}
});
};
this is my html code
<script type="text/ng-template" id="myModalContent.html">
<div class="loading-spinner">
<img ng-if="showLoading()" src="../../Content/images/loadingSpinner.gif" />
</div>
<div class="modal-header pad0">
<div class="col-lg-4 mrgt15 pad0"><h4 class="modal-title">{{objectName}}</h4></div>
<button class="btn btn-brand" type="button" ng-click="max()">max</button>
<button class="btn btn-brand" type="button" ng-click="min()">min</button>
</div>
<div class="modal-body table-responsive">
<div class="modal-inner list-modal-inner">
<div class="prefill-wrapper" ng-if="preFillListModel.data.length>0">
<div class="col-xs-12 col-sm-3 col-lg-2 pad0">
<label class="control-label">Prefill</label>
</div>
<div class="col-xs-12 col-sm-9 col-lg-5 pad0">
<select class="form-control"
ng-options="prefill.id as prefill.name for prefill in preFillListModel.data"
ng-model="prefill.id"
ng-change="getPrefillById(prefill,primaryObject);">
<option value=""> --Select-- </option>
</select>
</div>
</div>
<div class="clearfix"></div>
<div class="primary-record-panel">
<div>
<div ng-if="showColumns">
<div columns column-properties="primaryColumns" column-data="primaryObject">
</div>
</div>
</div>
<!-- Mid Content end -->
<!-- Content End -->
</div>
</div>
</div>
<div class="modal-footer">
<div class="pull-right col-lg-12 pad0">
<div class="col-lg-3 pad0 mart5"><label>Save as Prefill<input type="checkbox" ng-change="prefillCheckBox(isPreFill)" ng-model="isPreFill"></label></div>
<div ng-if="isPreFill" class="col-lg-4 mart5"><input type="text" ng-model="prefill.name" class="form-control" /></div>
<button class="btn btn-brand" type="button" ng-click="save(primaryObject,primaryColumns)">OK</button>
<button class="btn btn-primary" type="button" ng-click="clearFields(primaryObject)&&closeDailogue()">Cancel</button>
</div>
</div>
so on clicking the max button i want the popup to expand the width and height and same when i clikc on min button it should min to default hight and width,
is their any possibilities to do this?
You have to use two classes and use them in ng-class.
One class needs to set the width and height to 100% and the min button default height and width .
Additionally you can pass a parameter 'windowClass' specifying the width and height of the parent class.

Getting different value in manually eneterd/calculated

I have two input boxes: length and breadth.
The area is calculated is l*b and based on this I am calculating the result. My result that is Area to be treated:= (sum +(sum *0.05)).
It's working fine, but the problem is when I am entering area manually without length and breadth, I am getting different result. Both result must be same, even if I enter the area manually.
eg, if I enter the area as 1, the output is 10.05.
If I enter the length as 1 and breadth 1, the area is 1 so the output is 1.05 which is correct.
I need 1.05 whether I enter the length and breadth or just the area alone.
var app = angular.module('Calc', []);
app.controller('Calc_Ctrl', function ($scope) {
/* Start constants declaration*/
$scope.constant = {coeff : "0.003"};
/*End constants declaration*/
/*Start user input values and Function to add/remove input fields*/
$scope.choices = [{id : 'choice1', l2 : 0, b2 : 0}];
$scope.addNewChoice = function () {
var newItemNo = $scope.choices.length + 1;
$scope.choices.push({'id' : 'choice' + newItemNo, l2 : 0, b2 : 0});
};
$scope.removeChoice = function () {
var lastItem = $scope.choices.length - 1;
if (lastItem !== 0) {
$scope.choices.splice(lastItem);
}
};
$scope.sum = function () {
var sum = 0;
angular.forEach($scope.choices, function (choice) {
sum += choice.l2 * choice.b2;
});
return sum;
}
$scope.Getarea = function () {
$scope.total = document.getElementById("total").value;
//totalsum =$scope.total;
//alert(totalsum);
};
$scope.$watch($scope.sum, function (value) {
$scope.total = value;
});
/*End user input values and Function to add/remove input fields*/
});
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src ="area.js"></script>
<script type="text/javascript"></script>
<div class = "col-lg-8 col-md-7 col-sm-7 col-xs-12 center-output " ng-app="Calc" ng-controller="Calc_Ctrl" >
<h3 class="text-red bottom-line-thick">Antitermite treatment Calculator</h3>
<div class="col-md-12 col-sm-12 col-xs-12 step-2">
<div class="heading"><b>Step: 2</b> - Enter your <b>Requirement</b></div>
<!--Start Input calculation-->
<div data-ng-repeat="choice in choices" class="col-md-12 col-sm-12 col-xs-12 bottom-line">
<button type="button" class="btn btn-default pull-right btn-red" aria-label="Left Align" ng-show="$last" ng-click="removeChoice()">
<span class="glyphicon glyphicon-minus" aria-hidden="true" ></span>
</button>
<button type="button" class="btn btn-default pull-right btn-right-gap btn-red" aria-label="Left Align" ng-click="addNewChoice()">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</h4>
<div class="walls">
<div class="form-group col-md-4 col-sm-6 col-xs-6">
<label for="length">Length ({{data.selectedOption.name}}):</label>
<input type="number" class="form-control text-red bold" id="length" ng-model="choice.l2">
</div>
<div class="form-group col-md-4 col-sm-6 col-xs-6">
<label for="breadth">Breadth ({{data.selectedOption.name}}):</label>
<input type="number" class="form-control text-red bold" id="breadth" ng-model="choice.b2">
</div>
<div class="form-group col-md-4 col-sm-6 col-xs-6">
<label for="area">Area (sq {{data.selectedOption.name}}):</label>
<input type="number" class="form-control text-red bold" id="total" placeholder="Area" ng-model="total" ng-change="Getarea()">
</div>
</div>
</div>
</div>
<div class="col-md-12 col-sm-12 col-xs-12 step-result bottom-gap">
<div class="heading"><b>Step: 3</b> - Material <b>Required</b></div>
<div class="col-md-12 ">
<div class="col-md-4 col-sm-4 col-xs-4">
<p class="bold">Area to be treated:</p>
<h1>{{(total + (total * 0.05))|number:2}}<span class="small-text"> (sq {{data.selectedOption.name}}):</span></h1>
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
<p class="bold">ATT Chemical</p>
<h1>{{((total + (total* 0.05)) * constant.coeff) |number:2}}<span class="small-text">ltrs</span></h1>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
When you type in ng-model=total it will actually be treated a string. You could use input type=number perhaps, or you can just cast it to a number when you set the total:
$scope.Getarea = function () {
// numeric cast
$scope.total = +document.getElementById("total").value;
}
Since you already have ng-model you don't need the ng-change at all and you and use input type=number to have it work as a number or you can cast it where you actually use it.
{{((+total + (total* 0.05)) * constant.coeff) |number:2}}

select2 not working inside angularjs tabs

i have angularjs tab and i need select2 inside one of them.select2 woking in all of my page but not inside tab.
parts of tab code is:
<script type="text/ng-template" id="MetaIndicators.tpl.html" >
<div class="row col-xs-12" style=" margin-right:10px;margin-top:22px" >
<div class="form-group inline col-xs-12">
<div class=" form-group inline meta-form-groups col-xs-12">
<label class="metaLabels">ژانر</label>
<textarea name="Genre" data-kind="1" type="text" class=" form-control inline" ng-model="indicators1" ng-bind="indicators1" style="width:400px;float:right" readonly/>
<button class="btn btn-danger inline" style="margin-right: 35px;margin-top: 8px;" data-toggle="modal" data-target="#send-modal-IndicatorsGenre" ng-show="AllowEdit">ویرایش</button>
</div>
</div>
<div class="modal fade" id="send-modal-IndicatorsGenre" role="dialog" aria-hidden="true">
<div class="modal-dialog" style="background-color: white; margin-top: 100px;height: 179px; width:680px;">
<div class="modal-body" style="height:80%">
<form id="metaIndicatorsGenreInsert" class="form-horizontal" role="form">
<div class="col-xs-12" style="margin-top:8px;">
<label for="GenreID" class="col-xs-1 control-label" style="width:59px">ژانر</label>
<div class="col-xs-10">
<select multiple id="e1" style="width:500px">
<option></option>
<option ng-repeat="gn in GenreID" value="{{ gn.Key }}" ng-model="gn.Value">{{ gn.Value }}</option>
</select>
</div>
</div>
<hr class="stylish" />
<div class="col-xs-12" style="margin-top:20px;">
<i class="fa fa-mail-forward"></i> ثبت
انصراف
</div>
</form>
</div>
</div>
</div>
</script>
i add select2.min.js and select2.min.css to index .
function of fill select is :
function GetGenreListData() {
var Genre = metaService.CallService('get', "basic/GenreID", "");
Genre.success(function (data) {
$scope.GenreID = data;
}).error(function (data, status) {
alert($cookies.get('searchedMeta') + data.status);
});
}
and function of setting select2 is :
$("#e1").select2({
closeOnSelect: false
});
thanks alot

Resources