1

I have a scope array variable which i am trying to access dynamically. Its value has been already set.

Its like this.

$scope.setp = { arr: [] };
$scope.setp.arr[0] = "sample Value";

When I am trying to access it dynamically as below I get undefined.

console.log($scope['setp.arr[0]']);

However I am able to access it directly using the following.

console.log($scope.setp.arr[0]);

The way of dynamically getting value of scope variable works fine for others but fails when the variable name contains square brackets i.e. [ ].

I followed this example but no success for scope variable containing array or square brackets [ ].

Also dynamically setting of scope array variable using $parse service works fine as in below.

var scopeVariable = $parse('setp.arr[0]');
scopeVariable.assign($scope, "new Value");
2
  • $scope['setp'].arr[0] or $scope['setp']['arr'][0] if you want to go real cray cray. Commented May 12, 2016 at 12:03
  • Thanks a lot. It worked. Commented May 12, 2016 at 12:14

1 Answer 1

0

This won't work console.log($scope['setp.arr[0]']); as its trying to access a property setp.arr[0]. You can access it like console.log($scope['setp']['arr'][0]);

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

Comments

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.