1
{
"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.

6
  • Could you clarify what you mean by this? Commented Dec 9, 2015 at 11:36
  • can you please explain more about this ? Commented Dec 9, 2015 at 11:37
  • You have objects in an array. Your objects should have the same names for easier access. Commented Dec 9, 2015 at 11:39
  • check this once . hope it will be helpful Commented Dec 9, 2015 at 11:40
  • For example: "websites": [{ "url": "www.foo.com" }, { "url": "www.boo.com" }, { "url": "www.zoo.com" }] Commented Dec 9, 2015 at 11:41

3 Answers 3

2

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
      }
    });
Sign up to request clarification or add additional context in comments.

Comments

0
  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>

1 Comment

Object does not have all the three keys..It has any one of them!
0

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

Comments

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.