-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  
    };
8
  • how about $scope.basic={ name: $scope.name }; Commented May 24, 2017 at 6:08
  • Are you getting data from response? Commented May 24, 2017 at 6:10
  • 2
    Put $scope.basic=... inside callback Commented May 24, 2017 at 6:10
  • @FadiAboMsalam No. It not work. Commented May 24, 2017 at 6:12
  • @AlekseyL.can but I think it may look messy if I have a lot of function need to use data from the callback Commented May 24, 2017 at 6:14

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

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.
if your json is different you have to repeat that step again but with new param i mean "$scope.form2 = response".
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;
    });

2 Comments

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?
you can design the data structure as you wish since js does not care much what you do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.