How to access Angular JS inside string format - angularjs

Is there any way that I can get angular variable value as below syntax.?
var iNumber= "$scope.RegionLineNumber"

why you want to do this. If you want to get in string format then you can use
$scope.RegionLineNumber.toString();

You can use eval (read this and this before)
var iNumber = eval("$scope.RegionLineNumber");
however there's certainly a better way to do that. For example, if you know it's in $scope, just use
var iNumber = $scope["RegionLineNumber"];

Related

In a directive is it possible to target a variable on the scope using a string value?

I'm working on a directive and I need to set a variable on the scope, but all I have to work with is a string value that describes the path of the var:
var toChange = "targetObj['targetProp']";
I asked a question long ago regarding finding nested properties within a known object (and got a working answer), but haven't been successful leveraging that to look on the scope...is there a way to basically take that path and find it on the scope? This doesn't work but something along the lines of:
ERRONEOUS
scope.toChange = newValue;
Another approach is to use $scope.$eval:
var toChange = "targetObj['targetProp']";
var target = $scope.$eval(toChange);
target = newVal;
For more information, see
AngularJS $scope/$rootScope API Reference - $eval
Actually I figured it out, sorry everyone!
var x = $parse(toChange);
x.assign(scope, newVal);

AngularJS synchronisation between $scope and $localStorage

Slightly new to AngularJS so please bear with me.
I'm trying to implement ngStorage but had a doubt. If I assign something like this:
$scope.$storage = $localStorage.default({
var: 'foo'
});
And then if $scope.storage.var is changed when an AJAX call is made, does it also change the value of $localStorage.var or do I have to manually re-assign it?
If I do have to manually re-assign it, what's the best way to do so among the options below? (Please do let me know if there's any other way to do it)
$localStorage.var = 'foo2';
$localStorage.$reset({
var : 'foo2'
});
delete $localStorage.var followed by $localStorage.var = 'foo2'
Thanks in advance.
I just used the third option and it seemed to work correctly. That is:
delete $localStorage.var;
$localStorage.var = 'foo2';

Is AngularJS Filter dys'Functional'?

I'm new to Angular, and was curious why the difference in syntax:
$log.info("Hi");
So info is a function of $log and take a parameter string.
but...
$filter('uppercase')('Hi');
Why does "Hi" outside the function?
Why not $filter('uppercase','HI')
Whats happening here?
In this case $filter is a factory method. $filter('uppercase') creates the uppercase filter, which in turn is a method. Without assigning the created method to a variable you are calling the method in place.
It's more or less a shortcut for writing
var uppercaseFilter = $filter('uppercase');
var filteredString = uppercaseFilter(originalString);

Reactive non-mongo variable in angular-meteor

I couldn't find an answer or a solution to a challenge yet: How can I bind a variable (Session variable, no mongo collection) reactively in angular-meteor?
I'm converting from standalone meteor. Here I could use the template.helper method. As I can't use templates (and iron:router) anymore with angular-meteor and angularui-router, I can't bind reactivity to the helper anymore (at least in my understanding).
I tried this in an meteor-angular controller, which belongs to a sentence.tpl file:
$scope.parsestring = function(input_string){
tokenizer(input_string);
};
$scope.sentence_type = Session.getJSON("current_sentence.sentence_type");
Tokenizing works (I can see it in the debugger), but the value is only displayed, when I reload the page. What I want to achieve is tokenizing a string from an input field into a JSON representation (the tokenizer takes care of that) and displaying it similtaniously from the JSON representation in a structured way (separate html input elements, which are created dynamically). sentence_type is the variable that should be used on the html-page to show and change the sentence type, which can change while typing.
Anybody has some hints? Maybe, I could also use some Angular feature that I don't know?
Cheers,
Jan
Code repo:
My current code looks like this:
My code looks similar to this:
angular.module('ngaignt').controller("InteractorCtrl", ['$scope', '$meteor', '$meteorCollection',
function ($scope, $meteor, $meteorCollection) {
// Autorun is necessary to make reactive variables out of the JSON returns
var c = Tracker.autorun(function (comp) {
$scope.verb_type = Session.getJSON("current_verb.type");
$scope.object_type = Session.getJSON("current_object.type");
$scope.verb_attributes = _.toArray(Session.getJSON("current_verb.attributes"));
$scope.object_attributes = _.toArray(Session.getJSON("current_object.attributes"));
if (!comp.firstRun) {
// only do not do aply at first run becaulse then apply is already running.
$scope.$apply();
}
});
$scope.parsestring = function (input_string) {
interactor(input_string);
};
//$scope.on('$destroy', function () {c.stop()});
}]);
To use reactive variables, you need a reactive computation. You may need to use Tracker.autorun:
$scope.parsestring = Tracker.autorun(function(someStringInSession){
tokenizer(Session.get(someStringInSession));
});
Or you can use Tracker.autorun(func) wherever you use a reactive variable to reactively rerun a function when the variable changes.
good question and the best answer depend on your needs.
There are 2 possible solutions:
If you want to bind a Session variable to a scope variable, use the $meteorSession service.
What it does is that every time the scope variable will change, it will change to Session variable (and trigger an autorun if it's placed inside one).
and every time the Session variable will change, the scope variable will change as well (and change the view that it's placed upon).
If you are using the Session variable just to get a variable reactive (meaning trigger an autorun), you should use getReactively . this just returns the already existing scope variable but trigger an autorun every time it changes. a good example of this can be found it our tutorial.
Note: In anyway, when you use Tracker.autorun inside Angular, you need to connect it to a scope. this can be easily done if you replace Tracker.autorun with the $meteorUtils autorun function
Would be great if you could share a repo so that I can look on the broader perspective and could better determine what's the best solution from the two.
Based on another answer about "session" reacitivity, I could solve the problem. Just use the approach described in the link https://stackoverflow.com/a/21046935/4035797. You have to substitute Deps.autorun by Tracker.autorun though as Deps is deprecated and you have to make the scope variables for use in the template reactive (e.g., $scope.sentence_type = Session.getJSON("current_sentence.sentence_type");) and not the tokenizer.

Angularjs : How to find value of a variable store in a string

It seems novice question but I am not able to figure it out how to find value of a variable store in a string e.g
var value = "Scope.address";
Actually I want to find the value of "Scope.address" how to find it.
I am not able to understand what you want.
If you want to access a variable address(in angular this might be model) you have to use $scope.address.
Here is the example
<input type="text" ng-model="address">
and the corresponding script to access address in controller is
var value=$scope.address;
Updated
var value2=$scope[value];
Here is a demo
You have to reference $scope from a location where $scope is available. One common location is your controller
var value = $scope.address;
If you need it to be dynamic try the following:
var value = 'address';
var myDynamicValue = $scope[value];

Resources