AngularJs - access scope variable in ng-repeat with same name

shah

trying to access controller variable in ng-repeat with same name. e.g.

Controller

$scope.items = [1,2,3,4,5];

$scope.a = {
  data: "Some Data"
};

View

<div ng-repeat="a in items"> {{ a }} - {{ $parent.a.data }} </div>

but it is not working. is it possible to access that variable (Object) inside ng-repeat with same name?

Jacques Cornat

One way to make it, is accessing variables by naming your controller (Some example here in doc).

You can name your controller like ng-controller="TestCtrl as ctrl"

Then, you need some changes in your view :

<div ng-repeat="a in ctrl.items"> {{ a }} - {{ ctrl.a.data }} </div>

And in your controller :

app.controller('TestCtrl', function($scope) {
    var self = this;
    self.items = [1,2,3,4,5]; //brackets here, not curly brackets as said by @georgeawg 
    self.a = {
        data: "Some Data"
    };

    // Rest of you code
});

Finally, naming controllers is quite a good practice.

Hope it helps !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJs - access scope variable in ng-repeat with same name

From Dev

AngularJS: unable to access scope variable after ng-repeat in view

From Dev

AngularJS: variable in ng-repeat without $scope is in its local scope?

From Dev

AngularJS: Setting a variable in a ng-repeat generated scope

From Dev

AngularJS : variable of ng-repeat loop not updating $scope fast enough

From Dev

Angularjs scope variable with dot displays undefined within ng-repeat

From Dev

Building AngularJS variable name during ng-repeat

From Dev

Building AngularJS variable name during ng-repeat

From Dev

AngularJS nested ng-repeat scope

From Dev

AngularJS using ng-repeat on a $scope array

From Dev

AngularJS scope not binding data in ng-repeat

From Dev

AngularJS ng-repeat on directive and parent scope

From Dev

AngularJS : directive with ng-repeat and isolated scope

From Dev

scope is not working outside ng-repeat in angularjs

From Dev

Angularjs isolates scope directive with ng-repeat

From Dev

AngularJS nested ng-repeat scope

From Dev

AngularJS ng-repeat on directive and parent scope

From Dev

AngularJS using ng-repeat on a $scope array

From Dev

Angularjs - ng-repeat and nested scope

From Dev

iteration scope name in ng-repeat

From Dev

Dynamic Name for Scope in ng-repeat

From Dev

Access ng-repeat's item scope

From Dev

angularjs how to access ng-repeat filtered value outside its scope

From Dev

My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

From Dev

angularjs how to access ng-repeat filtered value outside its scope

From Dev

My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

From Dev

$scope.variable in ng-repeat

From Dev

ng-repeat variable scope to the DOM block

From Dev

$scope.variable in ng-repeat

Related Related

  1. 1

    AngularJs - access scope variable in ng-repeat with same name

  2. 2

    AngularJS: unable to access scope variable after ng-repeat in view

  3. 3

    AngularJS: variable in ng-repeat without $scope is in its local scope?

  4. 4

    AngularJS: Setting a variable in a ng-repeat generated scope

  5. 5

    AngularJS : variable of ng-repeat loop not updating $scope fast enough

  6. 6

    Angularjs scope variable with dot displays undefined within ng-repeat

  7. 7

    Building AngularJS variable name during ng-repeat

  8. 8

    Building AngularJS variable name during ng-repeat

  9. 9

    AngularJS nested ng-repeat scope

  10. 10

    AngularJS using ng-repeat on a $scope array

  11. 11

    AngularJS scope not binding data in ng-repeat

  12. 12

    AngularJS ng-repeat on directive and parent scope

  13. 13

    AngularJS : directive with ng-repeat and isolated scope

  14. 14

    scope is not working outside ng-repeat in angularjs

  15. 15

    Angularjs isolates scope directive with ng-repeat

  16. 16

    AngularJS nested ng-repeat scope

  17. 17

    AngularJS ng-repeat on directive and parent scope

  18. 18

    AngularJS using ng-repeat on a $scope array

  19. 19

    Angularjs - ng-repeat and nested scope

  20. 20

    iteration scope name in ng-repeat

  21. 21

    Dynamic Name for Scope in ng-repeat

  22. 22

    Access ng-repeat's item scope

  23. 23

    angularjs how to access ng-repeat filtered value outside its scope

  24. 24

    My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

  25. 25

    angularjs how to access ng-repeat filtered value outside its scope

  26. 26

    My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

  27. 27

    $scope.variable in ng-repeat

  28. 28

    ng-repeat variable scope to the DOM block

  29. 29

    $scope.variable in ng-repeat

HotTag

Archive