0

If have a straight ahead controller:

controller: function($scope, $http){
$http.get(templateSource+'/object.customer')
   .then(function(result){
      $scope = result.data;            
    });
}

My object.customer file looks like:

[{"customer":{"name":"Bert","email":"[email protected]"}}]

Now I wanted to access the email, so I did (in HTML)

{{customer.email}}

But this is incorrect, so my question is, how to access email?

UPDATE
Setting:

controller: function($scope, $http){
          $http.get(templateSource+'/object.customer')
           .then(function(result){
              $scope.customer = {};
              angular.extend($scope.customer,result.data[0]);
              console.log($scope.customer.email);
            });
        }

Gives me in the console.log unidentified. However, if I set it to console.log($scope.customer); I get:

Object 0: 
  Object customer: 
   Object email: "[email protected]"
          name: "Bert"

The console.log(result.data) is

[Object]
  0: Object
    customer: Object
        email: "[email protected]"
        name: "Bert"
4
  • You just overwrote your $scope. You need to do something like $scope.customers = result.data; Also, you are bringing back an array. If you intend to use the first item (after checking th elength), do $scope.customer = result.data[0]; Commented Feb 7, 2015 at 0:00
  • Hmm, this doesn't seem to work, I did an update though. Commented Feb 7, 2015 at 0:21
  • Can you post console.log(result.data) output? Commented Feb 7, 2015 at 0:31
  • Yes, please see the update in the original post Commented Feb 7, 2015 at 0:35

1 Answer 1

1

Use angular.extend

controller: function($scope, $http){
$http.get(templateSource+'/object.customer')
   .then(function(result){

         angular.extend($scope,result.data[0])            
    });
}

Then you can use $scope.customer.email. in you controller or {{customer.email}} in your html.

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

7 Comments

hmm, the output of console.log($scope.customer.email); = undefined
Maybe the update in my post gives a better understanding
@bvl oh you need an extra[0], check my answer. Can you post console.log(result.data) output?
That makes the array empty (console.log = Object {}).
@bvl oh, I got it, you need to apply extend to $scope not to $scope.customer.
|

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.