AngularJS - Get $scope variable from string -
AngularJS - Get $scope variable from string -
so have $scope
variable defined so:
$scope.data = { filter: { state: 'wa', country: 'us' } };
how access in separate service string? e.g. data.filter
in context of $scope
.
so have service method so:
function dosomething($scope, variablename) { // want access $scope[variablename] here?? }
i phone call controller so:
service.dosomething($scope, 'data.filter');
you want utilize $eval:
function dosomething($scope, variable) { var info = $scope.$eval(variable); // logs {state: "wa", country: "us"} console.log(data); }
however, if wanted functionality every time contents changes, preferable utilize $watch
function dosomething($scope, variable) { $scope.$watch(variable, function(data) { // logs {state: "wa", country: "us"} console.log(data); }); }
angularjs
Comments
Post a Comment