How to fill in and retrieve data from an input?


#1

Hello people.
I’m a beginner in ionic and I’m having trouble filling out a form.
In this form I send the data to the back end normally, the problem 'and when the user re-enters the app, I need this same form to come with the data already set in the values ​​of the respective inputs for visualization and later Update.
Could someone explain how I get this?


#2

Create Ajax request to retrieve data from back end, set the model values of the form control within the success of Ajax request. Should solve your problem


#3

I’m getting the values returned from the ajax and setting the inputs using “ng-value”, so they are displayed as expected, but when I do the submit data the data is sent, the form is sent with inputs without data, the only data that Are sent from inputs that undergo some insertion of values by the user.


#4

can you show your controller?


#5

In the controller I’m getting the data I previously saved in locastorage

 $scope.profile = JSON.parse(window.localStorage.getItem("profile"));

    $scope.preferences= {
                          phone: $scope.profile.u_phone,
                          state: $scope.profile.u_state
                  };

My HTML looks like this

<form name="frmProfile" class="vertical-align-content" ng-submit="save_preferences(frmProfile.$valid, data)" novalidate form-manager >            
   <label class="item item-input">
     <span class="input-label">Phone</span>
     <input type="tel" name="id_phone" id="id_phone" placeholder="Phone..." ng-value="profile.u_phone" ng-model="data.phone">
   </label>
   <label class="item item-input">
     <span class="input-label">State</span>
     <input type="tel" name="id_phone" id="id_phone" placeholder="State..." ng-value="profile.u_state" ng-model="data.state">
   </label>            
   <button type="submit" ng-disabled="!frmProfile.$valid" class="button button-block button-positive" data-ink-color="#9DEAFF" data-ink-opacity="1">Save</button>
 </form>

When I submit the data set in value are not sent, it is only sent if the user does some iteration with the input.

Below is an example of how I am trying to retrieve the sent data.

$scope.data = {};
    $scope.preferencias = function(validacao, data)
    {
       console.log($scope.data.phone);
       console.log($scope.data.state);
    }

#6

just try like this
$scope.profile = JSON.parse(window.localStorage.getItem(“profile”));

$scope.data= {
                      phone: $scope.profile.u_phone,
                      state: $scope.profile.u_state
              };

remove ng-value from view and use the ng-model only

$scope.preferencias = function(validacao, data)
{
   console.log($scope.data.phone);
   console.log($scope.data.state);
}

hope it will work