Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise
{
"websites": [{
    "A": "www.foo.com"
}, {
    "B": "www.boo.com"
}, {
    "C": "www.zoo.com"
}]

}

I have the above JSON array which my controllers inserts into the scope by doing the following:

$http.get('data/websites.json')
    .success(function (data) {
        $scope.websites = data;
    });

In my view, I have a variable that's either A, B or C. I just need to display the corresponding website in the view.

So - to sum it up:

In my view, I want to be able to do something like: {{websites.website.B}} and it would show the corresponding website URL.

share|improve this question
    
update your code – ngLover Dec 9 '15 at 11:35
    
Could you clarify what you mean by this? – user2536680 Dec 9 '15 at 11:36
    
can you please explain more about this ? – ngLover Dec 9 '15 at 11:37
    
You have objects in an array. Your objects should have the same names for easier access. – nipuna777 Dec 9 '15 at 11:39
    
check this once . hope it will be helpful – Shubham Dec 9 '15 at 11:40
up vote 1 down vote accepted

You have to loop through $scope.websites and match key with your view variable. If its matching then display value for that key.

Using something like:

    angular.forEach(obj, function(value, key) {
      if(key == <view variable>) {
      // use value to display appropriate website
      }
    });
share|improve this answer
  Try this code.

 <li ng-repeat="stuff in websites ">
    <a href="{{stuff.A}}" target="_blank">{{stuff.A}}</a>
    <a href="{{stuff.B}}" target="_blank">{{stuff.B}}</a>
    <a href="{{stuff.C}}" target="_blank">{{stuff.C}}</a>  
</li>
share|improve this answer
    
Object does not have all the three keys..It has any one of them! – Rayon Dabre Dec 9 '15 at 11:43

The short answer is you simply have to use ng-repeat to loop through your json content after you have fetched the data from your json content then simply interpolate the corresponding parts you wish to pull to your DOM elements (HTML) using the $scope service

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.