Select empties after clicking outside AngularJS/bootstrap - javascript

I'm new to AngularJS and currently i have a problem with a select.
I'm editing existing user info, and when it comes to choose a profile (perfil) or enterprise (empresa) it starts blank, that is not a problem, the problem is that if I click outside any input, the selected option vanishes FROM THE VIEW (i've checked and the data did bind).
Example:
The code it's here:
/*This is how i obtain the profiles from a table, this returns an array of objects which contains "codigo","descripcion","idPerfil"*/
function listaperfiles(){
var listaperf= portalServicios.listaperfiles(mntusuarioCtrl.id).$promise;
listaperf.then(function(respuesta){
mntusuarioCtrl.perfiles=respuesta;
});
listaperf.catch(function(respuesta){
$log.error("Perfiles Error");
$log.error(respuesta);
});
}
/*This function obtains a list of registered users from a table, this contains "nombre","apellidos","codigo","clave","idUsuario","tblPerfil","tblEmpresa", "estatus","email", "seleccionado" is just the user selected fomr that list that is going to be modified or deleted*/
function listaUsuarios(data){
var listausr = ssmeServicios.obtenusuarios(data).$promise;
listausr.then(function (respuesta){
$log.info(respuesta);
mntusuarioCtrl.usuarios=respuesta;
});
listausr.catch(function(respuesta){
$log.error(respuesta);
})
}
<div class="form-group">
<label for="perfil" class="col-sm-3 control-label" style="padding-right: 10px">Perfil:</label>
<div class="col-sm-9">
<select required="true"
id="perfil"
ng-model="mntusuarioCtrl.seleccionado.tblPerfil"
ng-options="opcion.descripcion for opcion in mntusuarioCtrl.perfiles">
<option value="" select hidden/>
</select>
</div>
</div>
it's just a minor nuisance but it has been bothering me!
If needed i can provide more info about the code or anything.
Thank you for your time!.

Here is a plunker with a select example, modified with a similar structure to your code:
http://plnkr.co/edit/sOqtxVz2utHUXhw0hOQd
Hope it helps! If not, please explain a little bit better your solution. A plunker could help a lot!
EDIT:
Now with your plunker I can see your error. Here is a forked plunker working good: http://plnkr.co/edit/sm1DhanxJpLJ4Yrkhucx?p=preview
Angular looks for object equality to bind it with your syntax and 2 objects are equal only if they point to the same reference. So it couldn't load the selected value. However, using track by solves this issue.

Related

AngularJS: Dynamically change select item of a dropdown list control using

I want to dynamically change select item of a dropdown list control using Angular JS, but it does not change the view.
I tried this one but no result yet!
Controller:
$scope.dischargeType = {id:0,title:''};
$scope.setSelect = function() {
debugger;
$scope.dischargeType = $scope.dischargeTypes[1];
alert($scope.dischargeType.title);
// it shows that the $scope.operationType get the value,but no changes on html
}
View:
<label class="col-sm-3 control-label">Discharge Type</label>
<div class="col-sm-9">
<select ui-select2 data-ng-model="dischargeType" id="add-dischargeType" name="dischargeType">
<option data-ng-repeat="item in dischargeTypes" value="{{item.id}}">{{item.title}}</option>
</select>
</div>
Thank you indeed.
From the code you posted... It doesn't look like you're actually calling the setSelect function in your code.
So basically you have a function made to do something, but if you never call it, it won't actually execute. Also, you may want to mention whether or not you're getting errors in the console window of chrome when you're viewing the page.

PHP/Javascript: Where to specify components of a drop down list?

I have recently been trying to alter someone else's code however am a complete beginner when it comes to PHP/Javascript. I want to add an extra option to a drop down menu that specifies the Read/Write nature of an object. Currently the only options are 'R' and 'W' however I would like to add an R/W option for Read/Write. To the best of my knowledge I believe the drop down list itself is instantiated here however please correct me if I'm wrong.
<div class="form-group">
<label class="col-sm-3 control-label">Read/Write:</label>
<div class="col-sm-3">
<select class="form-control" ng-options="item for item in readWrite" ng-model="singleRegister.RW" ng-change="modbusChange(modbusDetailsEdit)"> </select>
</div>
</div>
Where should I be looking in the code for the items in the list? I found this in a javascript file that seemed like what i was looking for:
$scope.readWrite=['R','W'];
However when i altered it to look as below nothing changed in the code
$scope.readWrite=['R','W','R/W'];
Any help would be much appreciated.
EDIT: This is the list in question FWIW...

Not able to set value to select tag using Angular js

My task is very simple. I'm able to set values to text boxes that come from restful api.. I'm able to get and display on the raw HTML page using {{currentCommodity.commodityName}} But, not able to set values to the select tag even though I'm able to get value.
My code is
$scope.editCommodity = function(commodityIndex) {
console.log("commodities object",$scope.commoditiesAdded[commodityIndex].commodityName);
$scope.currentCommodity.commodityName = $scope.commoditiesAdded[commodityIndex].commodityName;
}
It returns a commodity name with paddy..
My HTML code
---{{currentCommodity.commodityName}}-
<select class="form-control selection_size" ng-change="selectunits(currentCommodity.commodityName.commodityID)" ng-model="currentCommodity.commodityName" ng-disabled="{{editBusinessFlag}}" ng-options="commodityitem.commodityName for commodityitem in commodityList">
</select>
Any help would be greatly appreciated.
Try following code:
You selected : {{currentCommodity.commodityName}}
<select class="form-control selection_size" ng-model="currentCommodity" ng-options="commodityitem.commodityName for commodityitem in commodityList">
</select>

AngularJS SELECT doesn't validate properly

This is an AngularJS application (1.2.16). I browse to a dialog to edit some item. One of the controls is a multi-SELECT with the following visible values:
incident
work order
These visible values correspond to the following data values:
INCIDENT
WORK_ORDER
This is done through using the ng-options=" ... as ... for ... in ... " pattern, using an enumeration:
var FlexFieldSubjectTypeEnum = {
INCIDENT:{name:"INCIDENT", description:"incident"},
WORK_ORDER:{name:"WORK_ORDER", description:"work order"}
}
If have a form pretty much as follows:
<form ng-submit="save(formName)" name="formName" class="form-horizontal">
...
<div class="control-group">
<label class="control-label">Subject type:</label>
<div class="controls">
<select name="subjectType"
ng-options="type.name as type.description for type in getEnumAsArray('FlexFieldSubjectTypeEnum') | orderBy:'name'"
ng-model="entity.subjectType"
required></select>
</div>
</div>
Now, if the dialog loads the item ($scope.entity) from the backend and entity.subjectType is set to the first item in the list, the form validation marks it as unset. I have many other dialogs with similar constructs and have not seen this problem anywhere else.
If the item returned from the backend points to the second item (WORK_ORDER), this is nicely represented in the SELECT ("work order") and there is no validation error.
The problem does exist equally when using required or ng-required="true".
The problem does not exist if I remove the required attribute, but then the field also suddenly becomes optional, which is not what I wanted.
Your help much appreciated!
Almost a month later, with meanwhile an upgrade from Bootstrap v2.2.2 to v3.1.1 the problem disappeared.

Angularjs ng-model is undefined

In HTML I have this line of code:
<input type="text" class="training-comment" placeholder="Enter comment" ng-model="content" data-ui-keypress="{13:'keypressCallback($event)'}">
And in controller this:
$scope.keypressCallback = function ($event) {
console.log("comment is", $scope.content);
$event.preventDefault();
};
And when I enter some text in input and press enter in console I see that $scope.content is undefined.
Why is this ?
I put together a Plunker example here using the Angular UI and basically copying the code from the question. I then took this example and added an ng-repeat to demonstrate one of the most common issues I have seen: scope issues:
<div ng-repeat="x in collections">
<input type="text" class="training-comment" placeholder="Enter comment"
ng-model="content" data-ui-keypress="{13:'keypressCallback($event)'}" />
<br /><br />
</div>
You can find this updated plunker example here. Basically whenever you use an ng-repeat or any other directive that creates a new scope your model exists in that scope - not the parent or root scope. This means that your code might be working, but it is printing out the value from the wrong scope! For more information on scopes see the angular documentation here.
To use the plunker demo, type into the first input and press the enter key, the model will be updated. But, if you type into either of the other two boxes, though, the model will either not be updated or it will be undefined (if you have never typed into the first box).
Even if this isn't the exact issue, hopefully it will still help. Best of luck!

Categories