2

Display a scope variable which is similar to ng-repeat element in Angularjs

These are my scope variables

$scope.published = true;
$scope.count = 3;

I also have an array called labels

$scope.labels = ['published', 'count'];

In the view I want to view these data as the label name - label value.

<div ng-repeat="label in labels">
    {{label}} -  {{Here I want the value from the Scope variable}}
</div>

Can someone help me how to access the scope variable in this kind of scenario?

3
  • Assuming your HTML is within context of an ng-controller, which is assigned to the same controller containing your $scope variables, it should just be a matter of using {{count}} or {{published}} in your HTML if you want to display the values literally. If you've aliased your controller (e.g. ng-controller="Ctrl as foo") then you'll need to prefix the interpolations with the alias (e.g. {{foo.count}}). Commented Feb 14, 2020 at 3:41
  • Thanks But I want to display the value dynamically, because there are around 20 values which I need to iterate through ng-repeat, So just using {{count}} or {{published}} won't work in my use case. Commented Feb 14, 2020 at 3:49
  • Oops, I misread your question. Right, in that case, either map the assign the labels collection to objects which index the $scope properties, or have a function which returns the latter and make use of it with your ng-repeat (e.g. ng-repeat="obj in getObjs()"). If the labels collection ever varies, then the latter approach ensures you don't need to explicitly remap the labels. Commented Feb 14, 2020 at 4:24

2 Answers 2

4

Use the this identifier and bracket notation property accessors:

<div ng-repeat="label in labels">
    {{label}} -  {{this[label]}}
</div>

From the Docs:

It is possible to access the context object using the identifier this and the locals object using the identifier $locals.

For more information, see

The DEMO

angular.module("app",[])
.controller("ctrl", function($scope) {
    $scope.published = true;
    $scope.count = 3;
    $scope.labels = ['published', 'count'];
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
    <div ng-repeat="label in labels">
        {{label}} -  {{this[label]}}
    </div>
</body>

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

Comments

1

Map your labels to {key:"labelKey", value:"valueInScope"} so you can use them in the template.

Something like this could work

labels = ['published','count']
         .map(
             function(label){
                return {key:label,value:$scope[label]}
             }); 

then use

<div ng-repeat="label in labels">
   {{label.key}} - {{label.value}}
</div>

1 Comment

Thanks for your answer, I think this is also correct but it needs an extra map function

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.