Hmtl:
<div class="form-group">
<label for="loaisanpham">Loại Sản Phẩm</label>
<select class="form-control" name="idLoaisp" id="idLoaisp" ng-model="idLoaisp" ng-required=true>
<option ng-repeat="loaisanpham in loaisanphams | orderBy: 'tenLoaisp'" value="{{loaisanpham.idLoaisp}}">{{loaisanpham.tenLoaisp}}</option>
</select><br>
</div>
<div class="form-group">
<label for="phuong">Phường</label>
<select class="form-control" name="idVungxa" id="idVungxa" ng-model="idVungxa" ng-required=true>
<option ng-repeat="vungxa in vungxas | orderBy: 'tenVungxa'" value="{{vungxa.idVungxa}}">{{vungxa.tenVungxa}}</option>
</select><br>
</div>
JavaScript:
document.getElementById("idLoaisp").value = track.loaisanpham.idLoaisp;
document.getElementsById("idVungxa").value = track.vungxa.idVungxa;
Just idLoaisp show value in html and idVungxa not show.
I do not understand why it is not displayed. Help me. Tks all
Assign your value to the $scope in your controller.
$scope.idVungxa = track.loaisanpham.idLoaisp;
In plain HTML5 you would set the option as "selected":
like this:
<select multiple>
<option value="a">a</option>
<option value="b" selected>b</option>
<option value="c" selected>c</option>
</select>
There might be a different way to do it in Angular, but if this helps, you can use
<option [attr.selected]="boolean" ...
Remove the attribute "value" in the html options and set the value to the model, you have ng-model="idLoaisp" and ng-model="idVungxa" in the view so I guess in your controller you have that variables to store the values of the item selected, models are double binding that means you can also set the value to that model.
$ctrl.idLoaisp = track.loaisanpham.idLoaisp
$ctrl.idVungxa = track.vungxa.idVungxa
Related
<select name="repeatSelect" id="repeatSelect" ng-model="data.model">
<option ng-repeat="option in data.availableOptions" value="{{option}}">{{option.name}}</option>
</select>
The Above code works but it leaves me unable to retrieve the id field of the option element. See the different Plunker - working example with value={{option.id}} https://plnkr.co/edit/?p=preview - example with value={{option}} https://plnkr.co/edit/iEoHaSYLZbnwId8zHi9U?p=preview.
If I use ng-options in select it works - https://plnkr.co/edit/iEoHaSYLZbnwId8zHi9U?p=preview.
Do I need to filter ng-model?
You can't retrieve the id because by passing the option object in value you're converting it to a string, so data.model became "{"id":"1","name":"Option A"}". In order to pass the object use instead ng-value:
<select name="repeatSelect" id="repeatSelect" ng-model="data.model">
<option ng-repeat="option in data.availableOptions" ng-value="{{option}}">{{option.name}}</option>
</select>
What I understand from your question the below code should work
<body ng-app="ngrepeatSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="repeatSelect"> Repeat select: </label>
<!--<select name="repeatSelect" id="repeatSelect" ng-model="data.model" ng-options="option as option.name for option in data.availableOptions">-->
<!-- </select>-->
<select name="repeatSelect" id="repeatSelect" ng-model="data.model">
<option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
</select>
</form>
<hr>
<tt>model = {{data.model}}</tt><br/>
</div>
</body>
I am using angular2#2.4.1 release. How would i do a validation on select field?
<div class="form-group" [class.has-error] = "schoolError">
<label class="control-label" for="lang">Select School</label>
<select class="form-control" name="school"
required #school
[(ngModel)] = "model.school">
<option value="default">Select a school</option>
<option *ngFor= "let sch of school">{{sch}}</option>
</select>
</div>
<button class="btn btn-primary"
[disabled] = "form.invalid" type="submit">Submit</button>
Basically i want to disable the button when the select field is invalid? How do i make the select field invalid when no value is selected?
You can make a boolean variable and assign to it false as default value. When user chooses any option, it will turn true.
https://plnkr.co/edit/yR5xz4h3llkxHsUQxFJB?p=preview
<div>
<select [(ngModel)]='selected'>
<option value='one'>Three</option>
<option value='two'>Two</option>
</select>
<button [disabled]='!selected && form.status == 'VALID'>click</button>
</div>
selected:boolean = false;
Try changing the value in your default option to an empty string:
<option [value]="">Select a school</option>
I run into the same kind of situation and the below snippet seems to work pretty good.
<option [ngValue]="undefined">Select a school</option>
When adding ngForm to your name for your form your validation should work. I assume that is missing, because otherwise your form validation would work as is. So the only thing you would need is to add to your formtag is the aforementioned ngForm, like so:
<form #form="ngForm">
Here's a plunker
I believe you should be able to implement a [disabled]="checkSelectFunction()" on your button and then create a function to check if no value is chosen in the select field.
I am a little late here but the one thing that worked for me was setting the properties to null in the component.
For example,
HTML:
<div class="form-group">
<label for="type">Type</label>
<select
class="form-control"
id="type"
name="type"
[(ngModel)]="appointment.typeId"
required>
<option *ngFor="let aType of types"
[value]="aType.appointmentTypeId">
{{aType.type}}
</select>
Component:
appointment : Appointment = new Appointment(null);
Where model is:
export class Appointment {
constructor(
public typeId: number
) {}
}
I hope this helps someone like me who is new to Angular
I tried to give a value to option of the select like
<select id="something" formControlName="something">
<option value="something">Something</option>
</select>
The value of the option should not be an empty - if it's empty this required field counts as invalid.
So, I have the following code:
<input ng-model="refreshedByExample" type="text">
<select ng-model="example" ng-change="refreshedByExample = example">
<option value="1" ng-value="valueForRefreshedByExample1">1</option>
<option value="2" ng-value="valueForRefreshedByExample2">2</option>
<option value="3" ng-value="valueForRefreshedByExample3">3</option>
</select>
The value of the value's attribute is different than the value in ng-value. But, somehow, it is overrides and the value attribute gets the same value than ng-value. I get something like this:
<input ng-model="refreshedByExample" type="text">
<select ng-model="example" ng-change="refreshedByExample = example">
<option value="valueForRefreshedByExample1" ng-value="valueForRefreshedByExample1">1</option>
<option value="valueForRefreshedByExample2" ng-value="valueForRefreshedByExample2">2</option>
<option value="valueForRefreshedByExample3" ng-value="valueForRefreshedByExample3">3</option>
</select>
Is there a way to prevent this? I need to have on the value attribute a different value than in ng-value.
Any ideas? Thanks everyone.
i am getting a value in json andand want to put that value in selectbox html auto sleceted that in angularjs how can i do this?
Here is my html code
<select class="form-control" name="comregtype" ng-init="users.comregtype" id="comregtype" ng-options="c.name for c in colors" >
<option ng-selected="users.comregtype" ></option>
<option>Company Registration Type</option>
<option>Proprietor</option>
<option>Partnership Firm</option>
<option>LLP</option>
<option>Pvt. Ltd. Co.</option>
<option>Ltd. Co.</option>
</select>
JS: $scope.users = {comregtype:response.comregtype}
i am gettting value in response.comregtype = "LLP"
How can i set this as seleceted value in selecetbox.
if anyone can help .
Thanks
You can do this,
<select ng-model="value" ng-init="value='LLP'" ng-options="mode.value as mode.value for mode in types.RegistrationType">
</select>
DEMO
i have a conditional field like this :
<form ng-app="myApp" name="frm">
<select ng-model="NewBranchRequest.BranchTypeCode">
<option selected="selected" value="0">third</option>
<option value="1">first</option>
<option value="2">second</option>
</select>
<input class="ng-pristine ng-valid" type="text" name="power" ng-model="NewBranchRequest.PwrCnt" placeholder="sad" ng-required="NewBranchRequest.BranchTypeCode==0">
<span style="color:red" ng-show="frm.power.$invalid">
*
</span>
<button>Submit</button>
</form>
Demo
it works, but option fill from server and it doesn't work.
UPDATE
Check This Demo which is not working .
All i want is after select Bug in select list, input get rquired .
Any idea ?
In http://jsfiddle.net/sadeghbayan/MTfRD/1452/
change
ng-required="form.type==0"
to
ng-required="form.type=='bug'"
I made the changes here: http://jsfiddle.net/MTfRD/1453/
In addition, if you want to get the json object in form.type you can change this
ng-options='option.value as option.name for option in typeOptions'
to
ng-options='option.value for option in typeOptions'