-1

I need to fetch data via $http request and i need to use it for Edit value for my form.

Example code

 $http.get(base_url+"user/feach_one")
    .then(function (response) {$scope.json = response.data;
     $scope.name=$scope.json.name;
    });
 $scope.basic={
        name:// I want to get $scope.json.name here  
    };
Arun Sivan
  • 1,680
  • 2
  • 12
  • 23
Vuong Tran
  • 466
  • 1
  • 4
  • 12

2 Answers2

2

Bind your $scope to view, and create your form with your json params like this sample, read the comments

var app = angular.module("app", []);

app.controller("ctrl", function ($scope) {
   //json
   //uncomment in your local
   //$http.get(base_url + "user/feach_one").then(function (response) {
   //    var json = response;
   //    $scope.form = json;
   //});

   //we didn't have api here: { name: "Test", age: 20 }
   //comment in your local
   var json = { name: "Test", age: 20 }
   $scope.form = json;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <form>
      <label>name</label>
        <input type="text" ng-model="form.name"/>
      <label>age</label>
        <input type="text" ng-model="form.age"/>
  </form>
</div>
Maher
  • 2,517
  • 1
  • 19
  • 32
  • What if I have second form need to get data from $http request. example form2. Should I use $scope.form2 = json; Is there any way to reuse the data. – Vuong Tran May 24 '17 at 06:38
  • if your json is different you have to repeat that step again but with new param i mean "$scope.form2 = response". – Maher May 24 '17 at 06:47
0
$scope.basic= {};
$scope.advanced= {};
 $http.get(base_url+"user/feach_one")
    .then(function (response) {
     $scope.basic.name=$scope.json.name;
     $scope.advanced=$scope.json.avanced;
    });
Jester
  • 3,069
  • 5
  • 30
  • 44
  • it works! thank you But if I have other function need to use that data. example advanced. I need to put $scope.advance.name-$scope.json.name? – Vuong Tran May 24 '17 at 06:19
  • you can design the data structure as you wish since js does not care much what you do – Jester May 24 '17 at 06:35