Get input value and save it in local storage


#1

I’m trying to get the input box’s
value and store it into the local storage. The input is typed by user
and it’s refer to ip address.

Below is my html code:

<div>
<input ng-model="serverip">
<input type="button" class="button" value="Apply" ng-click="save()">
</div>

Below is my js code:

.controller('Ctrl', function($scope) {

         $scope.save= function() {
            console.log($scope.serverip);
            localStorage.setItem('serverip', $scope.serverip);
        };
 })

Why is it by using the above coding, after I key in the ip address into the input box, the $scope.serverip I get is always undefined?


#2

Here is an example how you can retrieve value for serverip:

Also, here is the full AngularJS form example:
https://docs.angularjs.org/guide/forms


#3

I think seems like the controller will not update the $scope.serverip value after I press the apply button.
If I write like this:

.controller('Ctrl', function($scope) {
         $scope.serverip = "888";
         $scope.save= function() {
            console.log($scope.serverip);
            localStorage.setItem('serverip', $scope.serverip);
        };
 })

I will always get the value 888 after I key in another value to the input box and press apply. Is it that ionic had cached the value?


#4

The problem is Javascript Prototypal Inheritance (not Ionic or AngularJS).
This document explains it better:

I’ve updated the example at: http://codepen.io/bokboki2002/pen/oXZZKL