Angular : value and selected not working in HTML - html

I am using an HTML form in my Angular project, but it keeps ignoring the "value" and "selected" parameters.
For the first case, I am trying to set the current timestamp as a default value for a datetime-local field.
The HTML code :
<div class="form-group">
<label for="timestamp_honeycomb">Date :</label>
<input type="datetime-local" class="form-control" id="timestamp_honeycomb" formControlName="timestamp_honeycomb" value="{{ timeElapsed | date: 'yyyy-MM-ddTHH:mm' }}" />
</div>
<div class="form-group">
<label for="smell_beehive">Odeur de la ruche</label>
<select name="smell_beehive" class="form-select-sm" id="smell_beehive" formControlName="smell_beehive">
<option value="faible" selected="selected">Faible</option>
<option value="moyenne">Moyenne</option>
<option value="forte">Forte</option>
</select>
</div>
The .ts code of the form :
formGroup = this.formBuilder.group({
timestamp_honeycomb: new FormControl([Validators.required]),
smell_beehive: new FormControl([Validators.required, Validators.pattern(this.regex)]),
)}
Thank you in advance.
EDIT : The "selected" part is solved, as I used this format :
smell_beehive: new FormControl('faible',[Validators.required, Validators.pattern(this.regex)])
Yet I remain curious about how to set a default value via the html view.

I found a way here : https://stackoverflow.com/a/51395993/9522006
Following that method, I then wrote my variables this way :
timeElapsed = Date.now();
formTime = this.datepipe.transform(this.timeElapsed, 'yyyy-MM-ddTHH:mm');
And, in the form declaration :
timestamp_honeycomb: new FormControl(this.formTime, [Validators.required])

Related

HTML + TypeScript : change value of field based on other field

I just started working with HTML and TypeScript and I'm stuck trying to set value of field depending on the value selected for another field in my HTML form.
HTML
<div class="form-group">
<label class="form-control-label" jhiTranslate="country.status" for="field_status">Status</label>
<select class="form-control" name="status" formControlName="status" id="field_status">
<option value="ACCEPTED">{{ 'CountryStatus.ACCEPTED' | translate }}</option>
<option value="CHECK_ONGOING">{{ 'CountryStatus.CHECK_ONGOING' | translate }}</option>
<option value="CHANGE_REQUESTED">{{ 'CountryStatus.CHANGE_REQUESTED' | translate }}</option>
</select>
</div>
<div class="form-group">
<label class="form-control-label" jhiTranslate="country.approvalDate" for="field_approvalDate">Approval Date</label>
<div class="d-flex">
<input id="field_approvalDate" type="datetime-local" class="form-control" name="approvalDate" formControlName="approvalDate" placeholder="YYYY-MM-DD HH:mm" />
</div>
</div>
WHat I want to achieve is following :
When I select status (Accepted), I want to have approval date field automatically set to now.
In case I select another status rather than Accepted, the approval date is cleared.
I assume I have to write a sort of function in my component.ts class but not sure.
Can you please give me some pointers / leads ? Thank you!

Using <p-dropdown> with form control

I think I am having an issue with value binding. I have 2 dropdowns on my page currently. The rest of the page is using PrimeNg for UI and would like to make these dropdowns look the same as the rest of the page. How should I go about making this work.
One dropdown is a supervisor list.
<div class="ui-g form-group">
<label for="supervisors">Supervisors * </label>
<select
class="form-control"
id="supervisors"
required
[(ngModel)]="model.supervisor"
name="supervisor"
>
<option *ngFor="let sup of supervisors" [value]="sup">
{{sup}}
</option>
<div
[hidden]="supervisors.valid || supervisors.pristine"
class="alert alert-danger"
>
Supervisor is required
</div>
</select>
</div>
The other is a leave code list
<div class="ui-g-12 ui-md-1" id="test">
<label for="codes">Leave Codes * </label>
<select
class="form-control"
id="codes"
placeholder="Select Leave Code *"
required
[(ngModel)]="model.code"
name="code"
>
<option *ngFor="let cod of codes" [value]="cod">{{cod}}</option>
</select>
</div>
I have 2 arrays of values being called from my .ts file
supervisors = ['Alex',"Jones",'Joe','Rogan'];
codes = ['Personal Leave','Vacation Leave', 'Sick Leave'];
When I use the tags I just get an empty drop down. I tried just using initially but then I was not able to get the required fields to validate.
Did you import DropdownModule?
import {DropdownModule} from 'primeng/dropdown';
See the documentation, html binding should be
<p-dropdown [options]="supervisorList" [(ngModel)]="supervisor"></p-dropdown>
where supervisorList will be defined as SelectItem in controller and needs to be in a label + value format.
supervisorList: SelectItem[];
this.supervisorList= [
{label: 'Alex', value: 'Alex'},
...
];

Angular 4 select element multiple value and

I am trying to add a select HTML element with multiple attribute set to it in a normal Angular 4 reactive form.
When checked in Chrome developer console, it shows following HTML:
Actual:
<select class="form-control ..." formcontrolname="..." id="..." multiple="" name="..." required="" ...>
<option value="0: 'MBA'" ng-reflect-value="MBA">MBA</option>
<option value="1: 'MSc'" ng-reflect-value="MSc">MSc</option>
</select>
Expected:
<select class="form-control ..." formcontrolname="..." id="..." multiple="" name="..." required="" ...>
<option value="MBA" ng-reflect-value="MBA">MBA</option>
<option value="MSc" ng-reflect-value="MSc">MSc</option>
</select>
Why is the value and ng-reflect-value different.
This is creating problem for me to get selected values, set default values, etc.
This does not happen when multiple attribute is removed.
Any idea what is going wrong here. Yes, I have google this issue but couldn't find any solution.
Edit:
In Component:
//variables
form: FormGroup;
degrees: FormControl;
degree_list = ['MBA', 'MCA', ...];
//through constructor parameters
private _fb = FormBuilder;
//in ngOnInit
this.form = this._fb.group({
...
degrees: this._fb.control('');
...
});
In Template file:
...
<select class="form-control" name="degrees" id="degrees" formControlName="degrees" multiple required>
<option *ngFor="let degree of degree_list" [value]="degree">{{degree}}</option>
</select>
...
According to this: https://github.com/angular/angular/issues/12074#issuecomment-251475820 you can initialize your formcontrol with an empty array. Also since you are using a reactive form, remove the required from your template and instead set the Validator when you build the form:
ngOnInit() {
this.form = this._fb.group({
degrees: [[''], Validators.required]
});
}
Also notice I removed this._fb.control I'm not sure why you are using that :)
StackBlitz

How to use ng-model in Angular.js?

I'm new to Angular.js . The problem is with my ng-model and ng-option. I have a data which shows code number with city name in my dropdownlist. I have made some changes to show only the city and remove the numbers in the dropdown list and it works fine Ex: CHENNAI. But, if i use ng-model and show the value in my Angular expression, still i'm getting the same data EX: 0123 CHENNAI. Using ng-model how can i show only the city name in the Angular expression i.e {{}}.
Here is the html:
<div class="form-group">
<label>Branch : <i class="mandate">*</i></label>
<select class="form-control input-md" name='branchName' ng-model="query.branch.branchName" ng-options="branch.branchName as showName(branch.branchName) for branch in baseBranches">
<option value="" selected>-- Select Branch --</option>
</select>
<span class="error" ng-show="search_form.branchName.$error.required">Branch is required</span>
</div>
And the script written to remove numbers:
$scope.showName = function(branchName){
return branchName.replace(/\d+/g, '')
//alert(branchName);
}
Please help from this because i'm new to Angularjs.
If you want both model value and text value that is shown to be without the number i.e "123CHENNAI" as "CHENNAI" use the same "showName" function you have written for the model value too as shown below :
<select class="form-control input-md" name='branchName' ng-model="query.branch.branchName" ng-options="showName(branch.branchName) as showName(branch.branchName) for branch in baseBranches">
<option value="" selected>-- Select Branch --</option>
</select>
Fiddle link here, Please check if that is what you are expecting.
Change your data into like
$scope.array =[{
number: 123,
name: "abc"
},
{
number: 123,
name: "abc"
}]

Hide seconds from datetime picker in front-end

I am using datetimepicker to display date and time and store it in a particular model in this format ("2015-01-01T15:10:23"), from this I want to hide seconds only in front-end, but not in back-end model.
Could anyone help me to solve my problem?
<div class="form-group" ng-class="{ 'has-error' : userForm.datetime.$invalid}">
<label>date and time</label>
<div class='col-sm-12 input-group date'>
<input id="datetimeid" ng-model="datemodel" name="datetime" type='text' class="form-control" ng-change="datevalidator()" placeholder="date and time" required/>
</div>
$scope.datevalidator = function () {
var datetime = $scope.datemodel;
$scope.datemodel = (datetime.indexOf(':00') > -1) ? datetime.replace(':00',"") : datetime;
};
You can use angular built-in filter:
{{ date_expression | date : format : timezone}}
Here you have more info on how to use it: https://docs.angularjs.org/api/ng/filter/date