AngularJS, how do you get a `select` to refresh automatically? - angularjs

I have a problem with 'select' when I choose an option in 1st 'select' I don't see the result in same time in the 2nd 'select', I must refresh the page manually with 'F5' so I can see the results.
add_parcelle.html
<div class="row">
<div class="col-md-12">
<div class="form-group" ng-class="{'has-error':Form.type.$dirty && Form.type.$invalid, 'has-success':Form.type.$valid}">
<label for="form-field-select-2">
Type de la parcelle <span class="symbol required"></span>
</label>
<select name="type" ng-model="type" title="select" class="cs-select cs-skin-elastic" ng-change="sendType(Form.type.$modelValue)" required>
<option value="" disabled selected>Selectionnez un type</option>
<option value="Lot">Lot</option>
<option value="Parcelle">Parcelle</option>
<option value="Zone">Zone</option>
<option value="Plant">Plant</option>
</select>
<span class="error text-small block" ng-if="Form.type.$dirty && Form.type.$invalid">Champ bligatoire</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group" ng-class="{'has-error':Form.idParcelle.$dirty && Form.idParcelle.$invalid, 'has-success':Form.idParcelle.$valid && Form.idParcelle.$dirty}">
<label for="form-field-select-2">
Parent de la parcelle <span class="symbol required"></span>
</label>
<select data-ng-init="showParents()" name="idParcelle" ng-model="idParcelle" title="select" class="cs-select cs-skin-elastic">
<option value="" disabled selected>Selectionnez un parent</option>
<option value="1">Aucun parent</option>
<option ng-repeat="p in parcelle" value="{{p.idParcelle}}"><div ng-if="p.parent.libelle">{{p.libelle}}</div></option>
</select>
<span class="error text-small block" ng-if="Form.idParcelle.$dirty && Form.idParcelle.$invalid">Champ bligatoire</span>
</div>
</div>
</div>
ParcelleController.js
$scope.sendType= function(type){
$sessionStorage.typeParc=type;
console.log($sessionStorage.typeParc);
};
$scope.showParents = function() {
if($sessionStorage.typeParc=='Parcelle')
$scope.showLots();
else if($sessionStorage.typeParc=='Zone')
$scope.showParcels();
else if($sessionStorage.typeParc=='Plant')
$scope.showZones();
};
In 1st 'Select' I use:
ng-change="sendType(Form.type.$modelValue)"
so I can store the Form.type and use it in function showParents() that show the result in 2nd 'Select'
data-ng-init="showParents()"
So, this is my problem!!
thanks in advance :)
after edit of 'Gustavo Gabriel'
this is the result:
enter image description here
I don't see the list in 'select' but I see it in console :p

In the first select modify your ng-change to:
ng-change="sendType(Form.type.$modelValue); showParents();"
Hope it helps =)

Related

Converting date formats with AngularJS

In my application I have 3 date formats PST, CST, and EST. When converting to IST the resulting date is 1 day earlier. Any suggestions?
#nalyd88 my html is
Select Date For Interview*
<div class="col-md-2">
<p>
<label>Start Time<label class="text-red" style="margin-bottom: 0px;">*</label></label>
<select class="form-control " ng-model="resume.interviewAvailability[0].from" required="required" ng-change="setEndTime(0);" style="margin-bottom: 23px;">
<option ng-repeat="time in startDate">{{time}}</option>
</select>
<select class="form-control " ng-model="resume.interviewAvailability[1].from" required="required" ng-change="setEndTime(1);" style="margin-bottom: 23px;">
<option ng-repeat="time in startDate">{{time}}</option>
</select>
<select class="form-control " ng-model="resume.interviewAvailability[2].from" required="required" ng-change="setEndTime(2);" style="margin-bottom: 23px;">
<option ng-repeat="time in startDate">{{time}}</option>
</select>
</p>
</div>
<div class="col-md-2">
<p>
<label>End Time<label class="text-red" style="margin-bottom: 0px;">*</label></label>
<select class="form-control " ng-model="resume.interviewAvailability[0].to" ng-change="checkInvalidEndTime(0);" ng-disabled="resume.interviewAvailability[0].invalid" required="required" style="margin-bottom: 23px;">
<option ng-repeat="time in endDate1[0]">{{time}}</option>
</select>
<select class="form-control" ng-model="resume.interviewAvailability[1].to" ng-change="checkInvalidEndTime(1);" ng-disabled="resume.interviewAvailability[1].invalid" required="required" style="margin-bottom: 23px;">
<option ng-repeat="time in endDate1[1]">{{time}}</option>
</select>
<select class="form-control" ng-model="resume.interviewAvailability[2].to" ng-change="checkInvalidEndTime(2);" ng-disabled="resume.interviewAvailability[2].invalid" required="required" style="margin-bottom: 23px;">
<option ng-repeat="time in endDate1[2]">{{time}}</option>
</select>
</p>
</div>
<div class="col-md-2">
<p>
<label >Time Zone<label class="text-red" style="margin-bottom: 0px;">*</label></label>
<select class="form-control" ng-model="resume.interviewAvailability[0].timeZone" required="required" style="margin-bottom: 23px;">
<option ng-repeat="timeZone in timeZones">{{timeZone}}</option>
</select>
<select class="form-control" ng-model="resume.interviewAvailability[1].timeZone" required="required" style="margin-bottom: 23px;">
<option ng-repeat="timeZone in timeZones">{{timeZone}}</option>
</select>
<select class="form-control" ng-model="resume.interviewAvailability[2].timeZone" required="required" style="margin-bottom: 23px;">
<option ng-repeat="timeZone in timeZones">{{timeZone}}</option>
</select>
</p>
</div>
</div>
<div class="row" style="margin-left: 0px;margin-right: 0px;margin-bottom: 8px;margin-top: 8px;">
<div class="col-md-3">
<h3 class="text-left text-primary">
Attachments
</h3>
</div>
and my controller is
(function(){
function applyJobController($scope,$state,openingsFactory,openingsService,$loading){
var today=new Date();
$scope.error="";
$scope.dateOptions={
"first":{
minDate: today
},
"second":{
minDate:today
},
"third":{
minDate: today
}
};
$scope.disabled = function(date, mode) {
return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
};
$scope.timeZones=["PST","CST","EST"];
$scope.startDate=["8:30 AM","9:00 AM","9:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM"];
$scope.endDate=["9:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM","07:30 PM","08:00 PM"];
$scope.resume={
"sections":[],
"interviewAvailability":[
{"from":$scope.startDate[0],
"to":$scope.endDate[0],
"timeZone":$scope.timeZones[0],
"invalid":false
},
{"from":$scope.startDate[0],
"to":$scope.endDate[0],
"timeZone":$scope.timeZones[0],
"invalid":false
},
{"from":$scope.startDate[0],
"to":$scope.endDate[0],
"timeZone":$scope.timeZones[0],
"invalid":false
}],
"attachment":"",
"attachmentName":"",
"notes":""
};
$scope.setEndTime=function(index){
$scope.resume.interviewAvailability[index].invalid=true;
if($scope.resume.interviewAvailability[index].from!=="Start Time"){
$scope.resume.interviewAvailability[index].invalid=false;
$scope.endDate1[index]=angular.copy($scope.endDate).splice($scope.startDate.indexOf($scope.resume.interviewAvailability[index].from));
$scope.resume.interviewAvailability[index].to=$scope.endDate1[index][0];
}
};
$scope.checkInvalidEndTime=function(index){
if($scope.resume.interviewAvailability[index].to==="End Time"){
$scope.resume.interviewAvailability[index].invalid=true;
}
};
};
applyJobController.$inject=['$scope','$state','openingsFactory','openingsService','$loading'];
angular.module('vResume.openings').controller("applyJobController",applyJobController);
})();
I am going to answer this with a generic suggestion. Moment is a tool to handle dates really well.
Note: in your time zone midnight (if you don't specify a time) may just be the day before in another time zone. It may be something like this that is causing you issues.

How to use required in ng-show?

I have a select box in that few values which is based on the selection i need to show the labels of the textbox.
ex: if you selected
the SE means I need to show the Location2 and Reporting To label box.
If you are selecting MANAger means need to show only Location2 label text
box only. And if you are selecting ADM/cluster, any one from this means need to be display only Location alone.
Js file
$scope.ChangeRole = function(id){
roleid=$scope.Role;
if(id=="2"){
// alert(id);
$scope.showreport =true;
$scope.showreportlocation=true;
$scope.showlocation = false;
}else if(id=="3"){
$scope.showreportlocation = true;
$scope.showreport = false;
$scope.showlocation = false;
}else{
$scope.showreport = false;
$scope.showreportlocation = false;
$scope.showlocation = true;
}
}
HTML file
<div class="col-lg-6">
<div class="form-group">
<label>Role<span style="color:red">*</span></label>
<div class="input-group role">
<select placeholder="Role" ng-model="Role" class="form-control pull-right" ng-change="ChangeRole(Role);" required ng-init="selectedType()" id="selectType"" name="role" required>
<option value="0">Role</option>
<option value="1">ADM</option>
<option value="2">SE</option>
<option value="3">MANA</option>
<option value="4">ClUSTER</option>
</select>
<div class="input-group-addon">
</div>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group" ng-show="showlocation">
<label>Location<span style="color:red">*</span></label>
<div class="input-group location">
<select id="LocationInput" class="form-control" ng-model="LocationId" ng-options="location.Id as location.NameoftheLocation for location in locations" required>
<option id="OptionInput" value="">Location</option>
</select>
<div class="input-group-addon">
</div>
</div>
</div>
</div>
<div class="col-md-6" ng-show="showreportlocation">
<div class="form-group">
<label>Location2<span style="color:red">*</span></label>
<div class="input-group site">
<select data-placeholder="Location" style="width: 100%;" ui-select2="select2Options" class="form-group" multiple ng-model="selectedTags" required>
<option ng-repeat="tag in tags" value="{{tag.Id}}">{{tag.NameoftheLocation}}</option>
</select>
<div class="input-group-addon">
<i class="fa fa-map"></i>
</div>
</div>
</div>
</div>
<div class="col-lg-6" id="otherType" ng-show="showreport">
<div class="form-group">
<label>Reporting To<span style="color:red">*</span></label>
<div class="input-group">
<select class="form-control" id="reportingto" name="reportingto" ng-options="n.Id as n.Name for n in names" ng-model="ReportingTo" required>
<option value="">Reporting To</option>
</select>
<div class="input-group-addon">
</div>
</div>
</div>
</div>
Here I have given the required option for all but which is not allowing me to save because required is present.
Using ng-show for particular selection I hide that label, but while saving html will check that field also, which is wrong please let me know that how to do.
Please some make the fiddle and let me know,
Use ng-required instead and only require it when it is shown.
I also tested Casey's suggestion of using ng-if instead of ng-show and that works as well. https://plnkr.co/edit/eeKvz5zRFeuLSF6fDbuy?p=preview

How can i do an ng-repeat by a given select option value

So my problem is as follows, i have this HTML
<div class="col-sm-4">
<p><span>Teste A/B:</span></p>
<label class="label-radio">
<input type="radio" class="radio" name="ab-test" value="true" ng-model="isTestAvailable"> Sim </label>
<label class="label-radio">
<input type="radio" class="radio" name="ab-test" value="false" ng-model="isTestAvailable"> Não </label>
</div>
<div class="col-sm-4">
<p><span>Nº de testes:</span></p>
<select class="form-control" ng-model="tests.number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
What i'm trying to do is make it so that when the user changes the select to another value i generate the given amount of test inputs via ng-repeat
<div ng-if="isTestAvailable == 'true'" ng-repeat="test in tests">
<hr>
<div class="row">
<div class="col-sm-12">
<h3>Test {{$index}}</h3>
<p><span>Specifications:</span></p>
<textarea id="teste1" class="form-control" onkeyup="resizeTextarea('test1')" data-resizable="true"></textarea>
</div>
</div>
<hr>
</div>
My controller contains this $watch
$scope.$watch("tests.number", function(newVal, oldVal) {
$scope.tests = [{}];
var i = parseInt(newVal);
for(var x = i; x <= newVal; x++) {
$scope.tests.push({});
}
});
that i tried to use in order to change the amount of test objects when the value changes, i tried using a simple variable as well and doing ng-repeat("i in tests.number") instead but that didn't work, what can i do so that the ng-repeat works with my given select option value?
I suggest to you, to use another variable instead of tests.number because you override it in your watcher.
Please check this plunker : https://plnkr.co/edit/XnV9MKjIYe1YOL4hR6Le?p=preview
I use another variable and bind ng-change instead of watch
<select class="form-control" ng-model="testNumber" ng-change="changeTestNumber()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

ui-select2 does not put the wanted value from code

I have few selects that looks like that:
<div class="row">
<div class="col-lg-4">
<label class="control-label" for="ModelSelect">Model </label>
<select ng-disabled="markedDic['make']!=true" class="form-control col-md-2" id="ModelSelect"
name="model" ui-select2
ng-model="carDetails.model">
<option ng-if="!carDetails.model" value="">[SELECT]</option>
<option ng-repeat="(model, value) in modelsDic" value="{{ model }}">{{ model}}</option>
</select>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<label class="control-label" for="VolumeSelect">Engine </label>
<select ng-disabled="markedDic['model'] != true" class="form-control col-md-2" id="VolumeSelect"
name="volume" ui-select2
ng-model="carDetails.volume">
<option ng-if="!carDetails.volume" value="">[SELECT]</option>
<option ng-repeat="(volume, value) in volumesDic" value="{{ volume }}">{{ volume }}</option>
</select>
</div>
</div>
As you can see, in every select the ng-disabled it dependent on markedDic[]
and the first option is following the same pattern
<option ng-if="!carDetails.volume" value="">[SELECT]</option>
When the form just loads, everything is fine, the default value is [SELECT] and then the rest of the values are the others.
But when I change in my javascript for example
carDetails.volume = "";
it makes the selected value in the select to be "" but still lets [SELECT] to be an option in the list.
While what I meant was that [SELECT] will be the option that is being showed as the selected value after my change in the js code.
What am I doing wrong?
Inside ng-if checking for not null and undefined can solve the problem. Try this because !"" will return truthy value.
ng-if="carDetails.model !== null && "carDetails.model !== undefined"
ng-if="carDetails.volume !== null && "carDetails.volume !== undefined"

Why is this form evaluating to true?

<form name="companyProfileForm" autocomplete="false" novalidate ng-submit="OnSaveCompanyProfile(companyProfileForm.$valid)" ng-controller="companyProfileDataEntryController">
<section ng-hide="!IsCompanyKnown();" class="row">
<div class="small-12 medium-6 large-6 columns">
<!--Industry-->
<section class="row">
<div class="small-12 medium-12 large-12 columns" ng-class="{ 'has-error' : (companyProfileForm.Industry.$invalid && submitted) }">
<div class="form-group">
<label for="Industry">*Industry</label>
<select name="Industry"
ng-model="CompanyProfileModel.IndustryId"
ng-options="item.Id as item.Description for item in IndustryList"
required>
<option value=null>-- select an industry --</option>
</select>
<span class="help-block" ng-show="companyProfileForm.Industry.$invalid && submitted">Please select a value.</span>
</div>
</div>
</section>
</div>
</section>
<button></button>
</form>
No matter what I do this form will evaluate to true. I want it to throw an error that Industry is required. Upon submit it goes to a function that prints to the console the parameter. It prints true every time.
You need to use the AngularJS ng-required directive, not required. Change the select element required to ng-required="true":
<select name="Industry" ng-model="CompanyProfileModel.IndustryId" ng-options="item.Id as item.Description for item in IndustryList" ng-required="true">
<option value=null>-- select an industry --</option>
</select>
Using 'required' on a form element will work in angular so long as your form element has an ng-submit="action()" and a button EXACTLY as follows inside the form:
<button type='submit' ng-click='submit()'>SomeText</button>

Resources