0

I'm wondering is there any way to add dynamically generated names in nested ng-repeat, for example:

    <div ng-repeat="x in JSONfile">
      <div ng-repeat="i in x.name">
         <span><a ng-href="{{i.link}}">{{i.name}}</a></span>
      </div>
    </div>

JSONfile: returns some names,

x.name is dynamically generated from the mentioned JSONfile, and it should be used as a plain text like "NAME", if I add NAME instead of i.name I get the json file loaded, but i want it automatically loaded, because I don't know which of the names will come first.

the i.name returns this:

n

a

m

e

not "NAME" as it should..

So, the question is, is there any way to tell angular that i want this dynamically generated value to be looked as I typed it?

PS. x.name loads a JSON file with some info about a person.

If i type ng-repeat="i in Tom" it will return the json, but with x.name it doesn't work.

Thanks!

EDIT (added json):

var brojVijesti = [ ];
$scope.JSONfile = brojVijesti;

            // LNG Json
            $http.get("/LEADERBOARDv2/jsons/LNG.php").then(function(response) {
                var LNG = response.data.LNG;
                $scope.LNG = LNG;
                $scope.LNGbroj = LNG.length;
                brojVijesti.push({"name":"LNG", "number":LNG.length});
            });

            // DT JSON
            $http.get("/LEADERBOARDv2/jsons/DT.php").then(function (response) {
                var DT = response.data.DT;
                $scope.DT = DT;
                $scope.DTbroj = DT.length;
                brojVijesti.push({"name": "DT", "number": DT.length});
            });
4
  • what is json sample format? Commented Feb 14, 2017 at 14:46
  • would you mind to share JSON? Commented Feb 14, 2017 at 14:46
  • 2
    why dont you just use x.name why r you looping around name? Commented Feb 14, 2017 at 14:47
  • @manish : I need to get LNG, or DT, from $scope. LNG and DT and many others, are generated dynamically. Commented Feb 14, 2017 at 15:06

1 Answer 1

3

I believe you want i in x not i in x.name

Edit: You can then use the $parse dependency which will grab the variable of that specific name from $scope.

<div ng-repeat="x in JSONfile">
  <div ng-repeat="i in x">
    <span><a ng-href="{{i.link}}">{{getVariable(i.name)}}</a></span>
  </div>
</div>

JS:

$scope.getVariable = function(variableName) {
   //evaluate the variable name in scope's context.
   return $parse(variableName)($scope); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

I want i in x.name, because that gives a list of names for the x in the first round of a loop.
@NicoLetterman So are you trying to get the name of "DT" and then call the $scope.DT variable in order to get the list of names found in "DT"/"LNG"?
Yep, exactly, that is what I want to achieve.
Thanks a lot! This is what I was looking for. :)

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.