I have a Problem with the AngularJS Scope. I want to use a Variable of it, which contains an Object. In the Object I want one index which is saved in another scope Variable.
So I want something like that:
{{graphdata.nodes._data.{{selectedElements.nodes[0]}} }}
graphdata.nodes._data is the Object, and the index I want is saved in selectedElements.nodes[0]. Can someone help me with that?
You cannot and shouldn't interpolate twice. just write:
{{graphdata.nodes._data[selectedElements.nodes[0]]}}
Related
I am trying to access an array of objects within $scope.
If I console.log($scope) (see image below) it displays the pedigree object that I would like to access. However, if I console.log($scope.pedigree) I get undefined. If I console.log(typeof $scope.pedigree) I get object.
Can someone help me access pedigree?
Pedigree is an array of objects, not a single object. As a result, you need to access the appropriate instance by index to get your object.
var pedigree = $scope.pedigree[0];
There is no "array" type in Javascript. As a result, when you use typeof on your $scope.pedigree property, it will tell you it is of type object, even though happens to be an array of objects.
You can access to scope via jquery or jqlite: $('.element').scope();
Accessing via console.log is not possible, because angular doesn't use global scope for storing scopes. More information here.
I have a constant app.constant('DOCUMENT_ID', window.documentId || 1); which I inject into services, controllers and directives.
I want to change DOCUMENT_ID to be a global variable, so that if I change DOCUMENT_ID's value in controller, I want to get the changed value in all services and controllers in which DOCUMENT_ID was injected.
How can I use a global variable in this way?
You don't need global variable here (plus it's a bad practice). Just make use of the fact that objects are passed by references. For example you could use a service defined like this:
app.value('DOCUMENT', {
ID: window.documentId || 1
});
Now, whenever you change DOCUMENT.ID anywhere, every part of the application will have updated version of ID. Just read it like object property.
Demo: http://plnkr.co/edit/Oay8IaLRnuCN2xkSuM69?p=preview
I have a variable param in scope as $scope.param that is always either foo or bar. I'm creating a table with rows defined by <tr ng-repeat="d in data">.
When I have the following: <td>{{d.foo}}</td> or <td>{{d.bar}}</td> everything works and the data shows up fine. However, when I have <td>{{d.param}}</td> angular can't find anything and the cell is blank.
In other words, I'm trying to access an object value using a variable as the key rather than the key itself. Any idea how to do this?
Use bracket notation:
{{d[param]}}
In my dustJS template, I make use of a combination of variable to use it as a new variable.
For example, say if I have pname and cname, then I want to create name = pname + cname.
Additionally.. I may want to create local variable based on certain condition, for example, name = {?.rep} pname {:else} pname + name {/.rep}. I may want to pass these variables into helpers.
To stick to the DRY principle, the best option would be to create local so that I can refer to this local variable later when it appears again.
I tried out with partials, something like: {<name}pname + cname{/name}, and use it later as {+name/}. But this way doesn't work with helpers, as well as the conditional block.
I think one possible solution might be that.. adding variables into the current context using helpers, but I don't know how to do.
So my question here is: how to add variable into current context, or how to define a local variable on the fly?
Thanks for your help.
#provide helper from dust-motes repo should help you solve this issue.
If for some reason #provide helper does not work for you, take a look at the proposed #addToContext helper in dustjs-helpers repo.#addToContext helper modifies context, so if you are not careful it might override context data with the same keys. #provide helper is preferred to #addToContext.
I've got a hbs template where I've got an array of objects and a boolean toggle variable (toggles the template behavior), let's say:
{
objs: list,
mode: true
}
I'm not able to access the mode variable when inside the loop over objs (the context is changed). What I want is to make an if-statement using the upper variable. I found that I can write a custom helper. But is there no other way to access the variable? I also found out, that inside the loop the variable is accessible via {{../mode}} - but still, don't know how to access that.
Finally, I've found a solution:
{{#if ../mode}}xyz{{/if}}