Angular ng-model not binding to select - angularjs

I'm pulling a list of accounts from my data base, and using that info to make a select drop down with the name showing, and id as the value. That all works fine, however, once I try to send it to my controller to be posted to my API, there's no to or from accounts. I have the ng-model on both fields, but it doesn't seem to do anything.
Here's my HTML
<form novalidate>
<div ng-controller="newTransactionController">
<div >
<label for="from">From Account</label>
<select name="from" ng-options="acct as acct.label for acct in options" ng-model="transaction.fromAccount" >
<option value="acct.value"> </option>
</select>
</div>
<div>
<label for="to">To Account</label>
<select name="to">
<option ng-repeat="account in accounts" value="{%account._id%}" ng-model="transaction.toAccount">{%account.name%}</option>
</select>
</div>
</div>
<div>
<label for="amount">Amount</label>
<input type="text" name="amount" id="amount" value="" tabindex="1" ng-model="transaction.amount"/>
</div>
<div>
<label for="currency">Currency</label>
<select name="currency" ng-model="transaction.currency">
<option value="USD">USD</option>
<option value="BTC">BTC</option>
</select>
</div>
<div>
<label for="interval">Interval</label>
<input type="text" name="interval" id="interval" value="" tabindex="1" ng-model="transaction.interval"/>
</div>
<div>
<label for="processDate">Process Date</label>
<input type="text" name="processDate" id="processDate" value="" tabindex="1" ng-model="transaction.processDate"/>
</div>
<div>
<input class="button" type="submit" value="Send" ng-click="createTransaction(transaction)"/>
</div>
</form>
And my controller
$http.get('').success(function(data){
$scope.accounts = data;
var options = $scope.options = [];
for(var i=0; i < data.length; i++){
$scope.options.push({label: data[i].name, value: data[i]._id});
}
});
$scope.createTransaction = function(transaction){
var transactions = {
fromAccount: angular.copy(transaction.fromAccount),
toAccount: angular.copy(transaction.toAccount),
amount: angular.copy(transaction.amount),
currency: angular.copy(transaction.currency),
interval: angular.copy(transaction.interval),
processDate: angular.copy(transaction.processDate)
};
$http.post('', transactions).success(function(){console.log('sent')});
};
When I log out transactions in the controller, all I get is: Object {amount: "1", currency: "USD"}
I'm lost. I can't figure out what's missing here.

Found my problem. It was a scope issue with the <div> tag surrounding both of the to and from accounts.

Related

Hide using ngClick in AngularJs

I have a table of profils, so to add one i click in a button, then setAdded() function show the form.
So what I want is when I select a multiple technologies the specific labels will be shown. I tried this but didn't work.
I have a form like this:
<form>
<div>
<input type="text"
ng-model="profil.libelleProfil_en" />
<label>Display</label>
<input type="checkbox"
ng-model="profil.displayProfil" />
<label>Technologie</label>
<select multiple>
<option value="{{tecno.idTechnologie}}"
ng-repeat="tecno in technologies"
ng-click="setClicked({{tecno.idTechnologie}})">
{{tecno.libelleTechnologie_en}}
</option>
</select>
<div ng-repeat="tecno in technologies">
<label ng-hide="tecSel{{tecno.idTechnologie}}">
{{tecno.libelleTechnologie_en}}</label>
<input ng-model="profil.libelleProfil_en" type="text"
ng-hide="tecSel{{tecno.idTechnologie}}" />
<label ng-hide="tecSel{{tecno.idTechnologie}}">
{{tecno.libelleTechnologie_en}} Profil Name</label>
<input ng-model="profil.libelleProfil_en" type="text"
ng-hide="tecSel{{tecno.idTechnologie}}" />
</div>
<button ng-click="addProfil(profil)">New Profil</button>
</div>
</form>
and in my script i have something like this:
$scope.setAdded = function(){
$scope.addState = !$scope.addState;
$scope.table = [];
angular.forEach($scope.technologies,function(value,key){
var j = value.idTechnologie;
$scope.table.push(j);
$scope.tecSel +""+ j == true;
});
};

Get value from ng-repeat key, value pair on POST Request

I am currently working on with a form that would get values from a json object. I'm done with it! (yey~) BUT when I POST the value I couldn't get anything. value="{{key}}" doesn't return a real data after POST. Any body can help me through solving this issue? Thanks alot!
<form class="form-horizontal" role="form" name="firewallForm" novalidate ng-init="firewallInit()">
<div class="form-group">
<label class="col-sm-1 control-label" for="firewallSource">Source</label>
<div class="col-sm-4 col-lg-2 col-md-2" ng-controller="entitiesController">
<select class="form-control input-medium" ng-model="firewallSource" ng-required="true">
<option value="" disabled selected> Enter Source Entity</option>
<option ng-repeat="(key, value) in entities.entities" value="{{key}}">{{key}}</option>
</select>
</div>
</div><div class="form-group">
<div class="col-sm-10">
<button type="button" class="btn btn-default" ng-click="submitFormFirewall()">
Submit
</button>
</div>
</div>
</form>
And my JS looks like:
$scope.submitFormFirewall = function() {
var postData = {};
postData["index"] = $scope.firewallRuleID;
postData["to"] = $scope.firewallDestination;
postData["application"] = $scope.firewallApplication;
postData["action"] = $scope.firewallAction;
postData["from"] = $scope.firewallSource;
Try this:
<select ng-model="firewallSource" ng-options="obj.key as obj.value for obj in entities.entities">
<option value=""> Enter Source Entity</option>
</select>
key and value are actually properties from your entities[0] object

bootstrap table is not working properly

ok, I have a issue with my bootstrap table. I have a Employee Modal with 2 tabs. The first tab is the table with the current Employees. When you select any Employee it takes you to the 2nd tab which has the Employee details. There is a input field on top of the table that allows searching, and if the Employee is not in there by the typed name, the user can hit Enter and it takes you to the details tab to create a new entry for that Employee .When I select a Employee and it takes me to the details tab, I want to be able to go back to the table and select a new Employee if need be. problem is that Employee is a empty row on the table. i need to reopen the modal for it to reappear.
plunkr
<div class="container">
<form class="form-horizontal" ng-submit="submitEmployee()" enctype="multipart/form-data">
<tabset>
<tab heading="List" select="ClearEmployeeModalFields();">
<div class="col-xs-12" style="margin-top:20px;width:initial">
<div style="overflow: auto;height:190px;max-width:520px;min-width:520px" id="scrollAreaCustomers">
<table class="table" style="">
<tr>
<th style="font-weight: bold;">Name</th>
</tr>
<tr>
<input type="text" placeholder="New Employee" ng-model="selectedEmployee.EmployeeFirstName" ng-enter="data.static = true" />
</tr>
<tr ng-repeat="employee in employeeArray | filter:selectedEmployee.EmployeeFirstName" class="pointer">
<td ng-click="setSelectedEmployee(employee);data.static = true">{{employee.EmployeeFirstName}} {{employee.EmployeeLastName}}</td>
</tr>
</table>
</div>
</div>
<!--End col-xs-12-->
</tab>
<tab heading="Details" active="data.static">
<div class="col-xs-12" style="margin-top:20px">
<div class="inline-fields" style="">
<label style="margin-left:-11px">Employee Id:</label>
<input style="width:100px" ng-model="selectedEmployee.CompanyEmployeeId" type="text">
<label style="margin-left:100px">Email:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeEmail" type="email">
</div>
<div class="inline-fields">
<label style="margin-left:0px">First Name:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeFirstName" type="text">
<label style="margin-left:57px">Title:</label>
<select style="width:150px" ng-model="selectedEmployee.EmployeeTitle">
<option value="" selected="selected">Select</option>
<option value="Manager" ng-model="selectedEmployee.EmployeeTitle">Manager</option>
<option value="Admin" ng-model="selectedEmployee.EmployeeTitle">Admin</option>
<option value="OfficeBitch" ng-model="selectedEmployee.EmployeeTitle">Office Bitch</option>
</select>
</div>
<div class="inline-fields">
<label style="margin-left:1px">Last Name:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeLastName" type="text">
<label style="margin-left:66px">PM:</label>
<select style="width:150px" ng-model="selectedEmployee.EmployeeIsPM" ng-options="o.v as o.n for o in [{ n: 'No', v: false }, { n: 'Yes', v: true }]">
<option value="true">True</option>
<option value="false">False</option>
</select>
</div>
<div class="inline-fields">
<label style="margin-left:0px">Cell Phone:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeCellPhone" type="text" ui-mask="{{'(999) 999-9999'}}">
<label style="margin-left:46px">Super:</label>
<select style="width:150px" ng-model="selectedEmployee.EmployeeIsSuper" ng-options="o.v as o.n for o in [{ n: 'No', v: false }, { n: 'Yes', v: true }]">
<option value="true">True</option>
<option value="false">False</option>
</select>
</div>
<div class="inline-fields">
<label style="margin-left:-14px">Office Phone:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeOfficePhone" type="text" ui-mask="{{'(999) 999-9999'}}">
</div>
</div>
</tab>
</tabset>
<!--End Tab Content-->
<br />
<div class="col-xs-12" style="margin-top: 220px;position:absolute">
<input style="margin-left: 3px; width: 70px" ng-click="updateEmployee(selectedEmployee)" type="button" value="Update" go-click="#" />
<input style="margin-left:285px;width:70px" type="submit" value="Save" go-click="#" />
<input style="margin-left: 20px; width: 70px" type="button" ng-if="true" data-dismiss="modal" value="Exit" go-click="#" />
</div>
Controller
//Sync Employee Table with Input Fields "New Employee Modal
$scope.setSelectedEmployee = function (employee) {
$scope.selectedEmployee = employee;
}
//Activate tab on selection
$scope.data = { static: false }
//GET Employees
EmployeeGet.query().then(function (data) {
$scope.employeeArray = data;
}, function (reason) {
errorMngrSvc.handleError(reason);
});
//Clear Employee Search Input Fields
$scope.ClearEmployeeModalFields = function () {
$scope.selectedEmployee.EmployeeCompanyId = '';
$scope.selectedEmployee.EmployeeFirstName = '';
$scope.selectedEmployee.EmployeeLastName = '';
$scope.selectedEmployee.EmployeeTitle = '';
$scope.selectedEmployee.EmployeeCellPhone = '';
$scope.selectedEmployee.EmployeeOfficePhone = '';
$scope.selectedEmployee.EmployeeEmail = '';
$scope.selectedEmployee.CompanyEmployeeId = '';
$scope.selectedEmployee.EmployeeIsSuper = '';
$scope.selectedEmployee.EmployeeIsPM = '';
};
I think the problem is when you are trying to do the object assignment. In Javascript, primitive types are copied by value and reference types are copied by reference.
$scope.setSelectedEmployee = function (employee) {
$scope.selectedEmployee = employee;
};
So when you deleting the $scope.selectedEmployee using $scope.ClearEmployeeModalFields function, it will delete the object in the array of $scope.employeeArray since it shares the same reference.
Try to replace your function with this to copy instead of pointing to same data:
$scope.setSelectedEmployee = function (employee) {
angular.extend($scope.selectedEmployee, employee);
};

How to pass ng-models to a form

i have a form with two differently named ng-models. I need to pass both of them with the form. How would this work? I need to pass currentItem + currentItem.Customer I am having trouble with a PdfSharp controller and I want to see if this is the reason why the Customer Values are being passed back as Null.
controller
$scope.EmailPdf = function () {
var id = $scope.currentItem.JobId
$http.get('/api/Pdf/' + id).success(function () {
$scope.PrintPreviewModal();
});
}
form
<form ng-submit="submitJob()" enctype="multipart/form-data" name="myForm">
<fieldset>
<div class="col-xs-12">
<label>Number:</label>
<input ng-model="currentItem.JobNumber" type="text" name="JobNumber">
<label>Customer:</label>
<input type="text" ng-model="currentItem.Customer.CustomerName"
typeahead="customer.CustomerName for customer in customerArray | filter:$viewValue"
typeahead-on-select="selectEditCustomer($item)">
</div>
<input ng-model="currentItem.CustomerId" type="text" ng-hide="true"/>
<div class="inline-fields">
<label >Status:</label>
<selectng-model="currentItem.JobStatus">
<option value="" selected="selected">Select</option>
<option value="Active">Active</option>
<option value="InActive">InActive</option>
<option value="Complete">Complete</option>
</select>
<label>Address:</label>
<input ng-model="currentItem.Customer.CustomerAddress" type="text">
</div>
<div class="inline-fields">
<label>Name:</label>
<input ng-model="currentItem.JobName" type="text">
<label>City:</label>
<input ng-model="currentItem.Customer.CustomerCity" type="text">
<label>St:</label>
<inputng-model="currentItem.Customer.CustomerState" type="text">
<label>Zip:</label>
<input ng-model="currentItem.Customer.CustomerZipcode" type="text">
</div>
<div class="inline-fields">
<label>Address:</label>
<input ng-model="currentItem.JobAddress" type="text">
<label>Ph:</label>
<input ng-model="currentItem.Customer.CustomerPhoneNumber" type="text">
<label>Fax:</label>
<input disabled style="width: 105px"ng-model="currentItem.Customer.CustomerFaxNumber" type="text">
</div>
<input ng-click="EmailPdf(currentItem)" type="button" value="Email" />
Well it's a bit difficult to tell exactly what you are asking here, but it sounds like you mean that you need to pass more information than the ID into your get. So you can pass the entire currentItem with all it's content like this
$http.get('/api/Pdf/' + id,angular.toJson($scope.currentItem)).....
Is that what you are trying to do?

How can I check number of elements on a select list and disable radio button?

I would like to:
disable radio button (with name 'advertisementType') when select list (with name 'updateAdvertisement') has got only 1 element
disable submit button (with name 'saveButton') when selected option on select list has got null value
How can I do that in angular?
This is my html:
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" value="false">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-show="advertisementType == 'false'">
<option value>Select item</option>
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
<option value="4">Audi</option>
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="saveButton" />
</form>
</div>
This is my javascript (but I have no idea):
<script>
function AdvertisementCtrl($scope) {
}
</script>
HTML
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" ng-disabled='carTypes.length<=1' value="false">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-options="item.type for item in carTypes">
<option value>Select item</option>
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="!updateAdvertisement" />
</form>
</div>
JavaScript
function AdvertisementCtrl($scope) {
$scope.carTypes = [
{id:'1', type:'Volvo'},
{id:'2', type:'Saab'},
{id:'3', type:'Mercedes'},
{id:'4', type:'Audi'}
]
}
The JSFiddle is here http://jsfiddle.net/7Jw9B/
Populate the select from an array that you declare in the controller
Check array length and disable the radio button when array length is 1
Disable saveButton based on the value of updateAdvertisement
<script>
function AdvertisementCtrl($scope) {
$scope.cars = ['Volvo', 'Saab', 'Mercedes', 'Audi', 'BMW'];
}
</script>
Markup:
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" value="false" ng-disabled="cars.length <= 1">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-show="advertisementType == 'false'" ng-options="cars">
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="updateAdvertisement" />
</form>
</div>
Should probably be tested in a fiddle :-), could do that later

Resources