1

In angular after making a GET request I have to do the following to match up all the values up.

 promise.then(function(object){

  $scope.name = object.name
  $scope.domain = object.domain
  etc

})

There must be a better way to do this so I don't have to hardcode all the values i.e.

$scope = object

or maybe a loop that matches them?

1 Answer 1

1

Yes, just assign that entire object to a $scope variable:

$scope.myObject = object;

Then reference in code {{myObject.name}} and so on.

Or, if you want to loop:

for (var key in object) {
    $scope[key] = object[key];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there not a way to do this without having to reference it inside another object. I would like to continue to use $scope.name, $scope.domain not $scope.myobject.name etc
can you not loop and assign the key values back somehow?
You can...Ill post an example of looping
thats the baby! thanks I could see that in my head but being new to this I was unsure. again thanks.

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.