AngularJS - Get $scope variable from string

Sam

So say I have a $scope variable defined like so:

$scope.data = {
    filter: {
        state: 'WA',
        country: 'US'
    }
};

How do I access that in a separate service by string? e.g. data.filter in the context of the $scope.

So say I have a service method like so:

function doSomething($scope, variableName) {
    // I want to access $scope[variableName] here??
}

I would call it from the controller like so:

service.doSomething($scope, 'data.filter');
Jonathan Gawrych

You will want use $eval:

function doSomething($scope, variable) {
  var data = $scope.$eval(variable);

  // this logs {state: "WA", country: "US"}
  console.log(data);
}

However, if you wanted to do some functionality every time the contents changes, it would be preferable to use $watch

function doSomething($scope, variable) {
  $scope.$watch(variable, function(data) {
    // this logs {state: "WA", country: "US"}
    console.log(data);
  });
}

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 - Get $scope variable from string

From Dev

Get dynamic scope array variable value in angularjs

From Dev

Get dynamic scope variable value angularjs

From Dev

Get dynamic scope array variable value in angularjs

From Dev

Setting a scope variable from a directive with AngularJS

From Dev

AngularJS $watch controller variable from a directive with scope

From Dev

AngularJS from $scope get form controller programmatically

From Dev

How to get plain object from $scope in angularjs?

From Dev

AngularJS from $scope get form controller programmatically

From Dev

AngularJs scope - how to get scope from DOM correctly?

From Dev

angularJS custom directive unable to show scope string variable

From Dev

Scope variable not printing AngularJS

From Dev

Variable scope issue with AngularJS

From Dev

Scope variable not binding in AngularJS

From Dev

Dynamic scope variable in angularjs

From Dev

AngularJS $scope variable only set once through HTTP GET

From Dev

Get AngularJS scope variable (inside javascript) defined in resolve/promise

From Dev

get echarts on click event to change value of variable attached to $scope in angularjs

From Dev

Prevent images from loading in an AngularJS view if scope variable is empty

From Dev

Is there a different way to hide a scope variable from showing while AngularJS is loading?

From Dev

Is there a different way to hide a scope variable from showing while AngularJS is loading?

From Dev

Accessing $scope variable from another method in the same controller Angularjs

From Dev

Passing String scope variables as arguments to $http.get in AngularJS

From Dev

Get confused by the scope in angularJS

From Dev

how to get data from controller to $scope in mvc using angularjs

From Dev

AngularJs get particular key value from JSON and store it to $scope

From Dev

AngularJS accessing scope from $http.get function

From Dev

Angularjs: Html value from string variable

From Dev

how to get variable from different AngularJS file?

Related Related

  1. 1

    AngularJS - Get $scope variable from string

  2. 2

    Get dynamic scope array variable value in angularjs

  3. 3

    Get dynamic scope variable value angularjs

  4. 4

    Get dynamic scope array variable value in angularjs

  5. 5

    Setting a scope variable from a directive with AngularJS

  6. 6

    AngularJS $watch controller variable from a directive with scope

  7. 7

    AngularJS from $scope get form controller programmatically

  8. 8

    How to get plain object from $scope in angularjs?

  9. 9

    AngularJS from $scope get form controller programmatically

  10. 10

    AngularJs scope - how to get scope from DOM correctly?

  11. 11

    angularJS custom directive unable to show scope string variable

  12. 12

    Scope variable not printing AngularJS

  13. 13

    Variable scope issue with AngularJS

  14. 14

    Scope variable not binding in AngularJS

  15. 15

    Dynamic scope variable in angularjs

  16. 16

    AngularJS $scope variable only set once through HTTP GET

  17. 17

    Get AngularJS scope variable (inside javascript) defined in resolve/promise

  18. 18

    get echarts on click event to change value of variable attached to $scope in angularjs

  19. 19

    Prevent images from loading in an AngularJS view if scope variable is empty

  20. 20

    Is there a different way to hide a scope variable from showing while AngularJS is loading?

  21. 21

    Is there a different way to hide a scope variable from showing while AngularJS is loading?

  22. 22

    Accessing $scope variable from another method in the same controller Angularjs

  23. 23

    Passing String scope variables as arguments to $http.get in AngularJS

  24. 24

    Get confused by the scope in angularJS

  25. 25

    how to get data from controller to $scope in mvc using angularjs

  26. 26

    AngularJs get particular key value from JSON and store it to $scope

  27. 27

    AngularJS accessing scope from $http.get function

  28. 28

    Angularjs: Html value from string variable

  29. 29

    how to get variable from different AngularJS file?

HotTag

Archive