0

This is my first test application I have been working on using AngularJS and I seem to hitting a few errors on simple things.

I have my AngularJS service which returns JSON to my Controller ('crucify' has been specified but this is whatever the user enters in the texbox):

{"crucify":{"id":37635889,"name":"Crucify","profileIconId":984,"summonerLevel":30,"revisionDate":1450980592000}}

Now in my Controller I want to be able to access the JSON values 'id' and 'name' etc. So I have wrote this:

xile.controller('searchController', ['personSearch', '$scope', function (personSearch, $scope) {

$scope.search = function () {

    var summoner = $scope.summoner;

    personSearch.findPlayer(summoner).then(function (data) {
        $scope.answer = data;
    });
}

}]);

So data is equal to the JSON posted above. How in my HTML do I bind the 'id' and 'name' from the JSON? I have posted my HTML below but this does not seem to work?

<div>Answer: {{answer.id}}</div>
<div>Answer: {{answer.name}}</div>

EDIT:

The user will enter there username in a textbox:

<input type="text" id="txt_searchUser" ng-model="summoner" />

So Crucify is an example and equals to whatever is entered in this box.

2
  • had to remove my answer since you're question is incomplete Commented Dec 24, 2015 at 18:35
  • @Leo Updated my question a little. Commented Dec 24, 2015 at 18:40

3 Answers 3

1

var key=Object.keys(data) //return ["crucify"], object key
$scope.summoner =data[key[0]]; //return object value

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

2 Comments

adding this worked. Thanks for your help :) Merry Christmas!
I'm glad to be useful. Merry Christmas!
0

xile.controller('searchController', ['personSearch', '$scope', function (personSearch, $scope) {

$scope.search = function () {

    var summoner = $scope.summoner;

    personSearch.findPlayer(summoner).then(function (data) {
     
      //first method
        $scope.answer = data.crucify;
      //second method
      $scope.answer = data
    });
}

}]);
//first method
<div>Answer: {{answer.id}}</div>
<div>Answer: {{answer.name}}</div>
//second method
<div>Answer: {{answer.crucify.id}}</div>
<div>Answer: {{answer.crucify.name}}</div>

1 Comment

Please read the question. I can't specify Crucify as this is whatever the user enters in the textbox. Crucify was an example.
0

Have you tried adding ng-if to your divs, like this?

<div data-ng-if="answer.id">Answer: {{answer.id}}</div>
<div data-ng-if="answer.name">Answer: {{answer.name}}</div>

1 Comment

I've just looked at your json a bit more carefully, you have a crucify nested, have you tried using answer.crucify.id ? edit: answer[summoner].id where summoner = 'crucify' for example

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.