localStorage value doesn't want to be set - angularjs

I have problem with set values in localStorage. I use AngularJS and Electron. When I update refreshTime, values for errors and opacity are set to null. Why?
$scope.storageLang = localStorage.getItem("language");
$scope.storageMonitors = localStorage.getItem("monitors");
$scope.storageAlarm = localStorage.getItem("alarm");
$scope.storageErrors = localStorage.getItem("errors");
$scope.storageDomain = localStorage.getItem("domain");
$scope.storageOpacity = localStorage.getItem("opacity");
$scope.refreshTime = localStorage.getItem("refreshTime");
$scope.submit = function($event) {
$event.preventDefault();
localStorage.setItem("domain", $scope.storageDomain);
localStorage.setItem("language", $scope.storageLang);
localStorage.setItem("monitors", $scope.storageMonitors);
localStorage.setItem("alarm", $scope.storageAlarm);
localStorage.setItem("errors", $scope.storageErrors);
localStorage.setItem("opacity", $scope.storageOpacity);
localStorage.setItem("refreshTime", $scope.refreshTime);
};
In HTML all my inputs has ng-model="storageLang" etc...
<form ng-submit="submit($event)" style="overflow:hidden">
<div class="field">
<label>{{ lang.domain }}</label>
<input type="text" ng-model="storageDomain" value="{{ storageDomain }}">
</div>
<div class="field">
<label>{{ lang.language }}</label>
<select ng-model="storageLang">
<option value="de" ng-selected="storageLang=='de'">Deutsch</option>
<option value="en" ng-selected="storageLang=='en'">English</option>
<option value="pl" ng-selected="storageLang=='pl'">Polski</option>
</select>
</div>
<div class="field">
<label>{{ lang.monitors }}</label>
<select ng-model="storageMonitors">
<option value="1" ng-selected="storageMonitors==1">1</option>
<option value="2" ng-selected="storageMonitors==2">2</option>
<option value="3" ng-selected="storageMonitors==3">3</option>
<option value="4" ng-selected="storageMonitors==4">4</option>
</select>
</div>
<div class="field">
<label>{{ lang.alarm }}</label>
<select ng-model="storageAlarm">
<option value="1" ng-selected="storageAlarm=='1'">{{ lang.yes }}</option>
<option value="0" ng-selected="storageAlarm=='0'">{{ lang.no }}</option>
</select>
</div>
<div class="field">
<label>{{ lang.errorslimit }}</label>
<input type="number" ng-model="storageErrors" value="{{ storageErrors }}" min="10">
</div>
<div class="field">
<label>{{ lang.opacity }}</label>
<input type="number" ng-model="storageOpacity" value="{{ storageOpacity }}" min="25" step="5">
</div>
<div class="field">
<label>{{ lang.refreshTime }}</label>
<input type="number" ng-model="refreshTime" value="{{ refreshTime }}" min="10" step="1">
</div>
<button class="button expanded neutral">{{ lang.save }}</button>
</form>

Problem was that I use value="{{ storageOpacity }}" and I just need to use ng-model. Beside this I need to use parseInt for some integer values...

localStorage can only handle strings so setting integers is going to cause issues.
Use a library like localForage which has the same API but will use IndexedDb in Electron and handles other data types.

Related

Using ng-model and ng-if to show elements based on value - Angular 2

So in AngularJS i was using the following to show specific form elements based on a value that is captured by a model:
<fieldset class="full-width sm-padding">
<label>Do you have any major medical conditions such as
heart conditions, cancer or diabetes?</label>
<div class="inline-flex">
<input type="radio" name="medicalCondition" value="yes"
tabindex="16" ng-model="clientDetails.medicalCondition">Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="medicalCondition" value="no"
tabindex="17" ng-model="clientDetails.medicalCondition">No<br>
</div>
</fieldset>
<fieldset class="full-width sm-padding" ng-
if="clientDetails.medicalCondition == 'yes'">
<label>Are you currently taking any medication or do you
have any other medical conditions? For example HBP, Cholesterol, Asthma,
Depression? (Both lives if cover for couple)</label>
<div class="inline-flex">
<input type="radio" name="otherMedicalCondition"
value="yes" tabindex="18" ng-
model="clientDetails.otherMedicalCondition">Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="otherMedicalCondition"
value="no" tabindex="19" ng-
model="clientDetails.otherMedicalCondition">No<br>
</div>
</fieldset>
How can I do the same thing in Angular 2?
Have tried this but not working:
<fieldset class="full-width sm-padding">
<label>Do you smoke?</label>
<div class="inline-flex">
<input type="radio" name="smoker" value="yes"
tabindex="12" [(ngModel)] = "clientDetails.smoker">Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="smoker" value="no" tabindex="13" [(ngModel)] = "clientDetails.smoker">No<br>
</div>
</fieldset>
<div class="full-width flex" *ngIf="clientDetails.smoker ===
'Yes'">
<fieldset class="one-quarter sm-padding">
<label>What do you smoke?</label>
<input list="whatDoYouSmoke" name="whatDoYouSmoke"
tabindex="14">
<datalist id="whatDoYouSmoke">
<option value="Cigarettes">
<option value="Cigars">
<option value="Pipe">
<option value="E-Cigs">
<option value="Other">
</datalist>
</fieldset>
<fieldset class="one-quarter sm-padding">
<label>How many per day?</label>
<input type="number" min="0" name="howManySmokesPerDay"
tabindex="15">
</fieldset>
</div>
What am I doing wrong?
Have tried so many different options but can't quite get it to work!
thanks
The only thing I can see is that you have typed [(ngModel)] = "", try without spaces like: [(ngModel)]="". Also noticed that in your *ngIf="clientDetails.smoker ===
'Yes'", try with a little 'y' in yes as: *ngIf="clientDetails.smoker ===
'yes'". Since you are setting the value to "yes" and not "Yes" in your radio button.
There is a case problem in the Angular version. The first 'yes' starts with a lower case y and a second 'Yes' with a upper case Y.
For anyone out there looking for a solution to this, I ended up doing the following:
<fieldset class="full-width sm-padding">
<label>Do you smoke?</label>
<div class="inline-flex">
<input type="radio" name="smoker" [(ngModel)]="smoker"
[value]="true" [checked]="smoker" /> Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="smoker" [(ngModel)]="smoker"
[value]="false" [checked]="!smoker" /> No<br>
</div>
</fieldset>
<div class="full-width flex" *ngIf="smoker">
<fieldset class="one-quarter sm-padding">
<label>What do you smoke?</label>
<input list="whatDoYouSmoke" name="whatDoYouSmoke" tabindex="14">
<datalist id="whatDoYouSmoke">
<option value="Cigarettes">
<option value="Cigars">
<option value="Pipe">
<option value="E-Cigs">
<option value="Other">
</datalist>
</fieldset>
<fieldset class="one-quarter sm-padding">
<label>How many per day?</label>
<input type="number" min="0" name="howManySmokesPerDay"
tabindex="15">
</fieldset>
</div>

Clear form using angularjs

Problem: I am unable to clear the fields of the form completely.
Let me be more specific. When I go to the console, it appears that the previous entries are still present even though the reset button has been selected.
I have done the following in the form. I have a submit button and a reset button.
<div class="panel panel-default">
<div class="panel-body">
<form name="providerSearch" ng-submit="SearchProvider(searchParam);" novalidate role="form">
<div class="form-group"><input class="form-control" id="physiciansfirstname" ng-model="searchParam.FirstName" placeholder="First name:" type="text" /></div>
<div class="form-group"><input class="form-control" id="physicianslastname" ng-model="searchParam.LastName" placeholder="Last name:" type="text" /></div>
<!---<div class="form-group">
<select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty">
<option disabled="disabled" selected="selected" value="">Specialty</option>--->
<!---<option disabled="disabledvalue=""></option>---><!---<option>Family practice</option><option>General practice</option><option>Internal medicine</option><option>Pediatrics</option>
</select>--->
<div class="form-group">
<select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty">
<option selected="selected" value="">Specialty</option>
<!---<option value=""></option>--->
<cfoutput query="SpFind">
<option value=#ProviderSpecialty#>#ProviderSpecialty#</option>
</cfoutput>
</select>
</div>
<div class="form-group">
<select class="form-control" id="city" ng-model="searchParam.City">
<option selected="selected" value="">City</option>
<!---<option value=""></option>--->
<cfoutput query="cityFind">
<option value=#city#>#city#</option>
<!---<option ng-selected="{{searchParam.City==#city#?true:false}} value=#city#>#city#</option>--->
</cfoutput>
</select>
<!---<select class="form-control" id="city" ng-model="searchParam.City"><option disabled="disabled" selected="selected" value="">City</option><option ng-repeat="c in Cities" value="{{c.City}}">{{c.City}}</option> </select>--->
</div>
<div class="row">
<!---<div class="col-xs-6 no-right-padding paddingLanguage">
<div class="form-group widthLanguage">
<select id="language" name="language" class="form-control" ng-model="searchParam.Language">
<option disabled="disabled" selected="selected" value="">Language</option>
<!---<option value=""></option>--->
<cfoutput query="Languages">
<option value=#Language#>#Language#</option>
</cfoutput>
</select>
<!---<select name="language" class="form-control widthLanguage" id="language" ng-model="searchParam.Language">
<option disabled="disabled" selected="selected" value="">Language</option>
<option ng-repeat="l in Languages">{{l.Lang}}</option>
</select>--->
</div>
</div>--->
<div class="col-xs-6 no-left-padding">
<div class="form-group"><select class="form-control" id="gender" name="gender" ng-model="searchParam.Gender">
<option selected="selected" value="">Gender</option>
<!---<option value=""></option>--->
<option>Male</option><option>Female</option> </select></div>
</div>
</div>
<hr class="hrDoctor" />
<div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div>
<div class="row">
<div class="col-xs-7 no-right-padding">
<div class="form-group">
<div class="input-group">
<select class="form-control" id="miles" name="distance" ng-model="searchParam.Distance">
<!---<option selected="selected" value="" disabled="disabled">Miles</option>--->
<option selected="disabled" value=""></option>
<option value={{v.value}} ng-repeat="(k , v) in miles track by $index">{{v.value}}</option>
<!---<option selected="disabled" value=""></option>
<option selected="selected" value="5">5</option>
<option selected="selected" value="10">10</option>
<option selected="selected" value="15">15</option>
<option selected="selected" value="20">20</option>--->
</select>
<div class="input-group-addon">mi</div>
</div>
</div>
</div>
<div class="col-xs-5 no-left-padding widthZip">
<div class="form-group">
<input allow-pattern="[\d\W]" class="form-control" id="zip" maxlength="5" ng-model="searchParam.Zip" placeholder="Zip code" type="text" />
</div>
</div>
</div>
<div class="form-group">
<input class="btn btn-warning btn-block" onclick="return checkTextField(); overlayDisplayButton();" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" />
<div style="margin-top:10pt"><button type="reset" class="btn btn-info btn-block" ng-click="reset()">Reset</button></div>
</div>
</form>
</div>
Here is the following script to reset the form:
$scope.reset = function(form) {
form.$setPristine();
form.$setUntouched();
};
What am I missing to reset the form completely?
This should be your reset function:
$scope.reset = function() {
$scope.searchParam = {};
$scope.providerSearch.$setPristine();
$scope.providerSearch.$setUntouched();
};
where providerSearch is a form name.

Data not being populated in drop down from JSON Response from REST in Angular js

I'm getting a JSON value from one WebService like this
"electricity":
{ "selectedElectricityBoard":"ABCD",
"electrictyConsumerNumber":"12345",
"isElectricityBoardChecked":true
}
I generated a Drop Down list from the JSON data,after that i select the value and submit the value, the value gets submitted successfully from my UI, now the case is I'm editing the same form for which i'm making a Web Service call which returns me the same value, which i have selected from the UI, but the selected value is not being populated/reflected in the drop-down menu, any suggestion or help would be highly appreciable, My UI is in Angular JS, the code snippet is as follows :
<div class="col-xs-2 no-padding ">
<label>Consumer No.:</label>
</div>
<div class="col-xs-3">
<span ng-hide="true" class="form-control" ng-required="true"
close-text="Close">
</span>
<div class="group-3-text-boxes col-xs-3 no-padding" style="min-width: 100px;">
<input type="text" name="consumer_number"
ng-disabled="!form_data[selectedTab].electricity.isElectricityBoardChecked"
ng-model="form_data[selectedTab].electricity.electrictyConsumerNumber"
class="input-field"
ng-class="{true:'invalid-field',false:''}
form_data[selectedTab].electricity.isElectricityBoardChecked && form_data[selectedTab].invalid.electricity]" required />
<html ng-app="app" ng-controller="ctrl">
<select id="state" ng-model="states" ng-options="state for (state,states) in total_states">
<option value=''>Select State</option>
</select>
<select id="district" ng-model="districts" ng-options="district for (district,districts) in states">
<option value=''>Select District</option>
</select>
<select id="city" ng-model="cities" ng-options="cities for cities in districts">
<option value=''>Select City</option>
</select>
<br>
<br>
<br>
State :: {{states}}
<br>
<br>
<br>
District:: {{districts}}
<br>
<br>
<br>
City :: {{cities}}
for controller:
app.controller('ctrl',ctrl);
ctrl.$inject=['$scope'];
function ctrl($scope) {
$scope.total_states = {
'Andhra Pradesh': {
'Prakasam': ['Ongole', 'Chirala', 'Mrakapuram'],
'Krishna': ['Vijayawada', 'Machalipatnam'],
'Guntur': ['Guntur', 'Tenali', 'Bapatla']
},
'Telangana': {
'Hyderabad': ['Hyderabad'],
'Warangal': ['Wrangal', 'Hanmkonda'],
'Khammam': ['Khmammam', 'Badrachalam']
}
};
}
try this it works...

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>

Angularjs ng-disabled + radioSelected

<input type="radio" name="ptype" value="{{a}}" ng-model="group" checked>
<div class="row" id="{{a}}">
<div class="form-group">
<select class="form-control" ng-model="group" value="1" id="btype" ng-options="idBankCard as vcBankCardDesc.vcDescription for (idBankCard, vcBankCardDesc) in b.status" name="idGroup" ng-disabled="group== 2">
<option value="">--Please Select Payment--</option>
</select>
</div>
</div>
I have two select options. When I select option 1 then option 2 should be disabled. I tried many way but still cannot work...Anyone can give me solution?

Resources