Connect two scopes in AngularJS - kind of

So I'm just starting out with AngularJS and I've got two scopes defined in a controller. In the view I want to have a sort of lookup to the second scope. How's that done?

So more or less - the variable at..

<div class="panel-note">{{ servers.install_id }}</div>

Open in new window

needs to be replaced with {{ notes.installation }} where = servers.install_id

I hope it makes sense?
Who is Participating?
I suppose you are actually referring to two variables in a scope when you are saying "defined two scopes". Assuming - a lot - and that the scopes are something like

$scope.servers = { 'install_id': 1 };
$scope.notes = { 'id': 1, 'installation': "" };

You can either use the ng-if, ng-view directives to eliminate, show and hide elements. Something like

<div ng-if="servers.install_id ==" class="panel-note">{{ servers.installation }}</div>
<div ng-if="servers.install_id !=" class="panel-note">{{ servers.install_id }}</div>

In this markup, only one div will be visible at a time.


If this is to be needed in a lot of places, then you should consider either using a computed variable or a directive.
Kyle HamiltonData ScientistCommented:
not sure how you can have two scopes in one controller.  a controller has one scope - that's kind of a basic premise of angular. there are situations where you might end up with some nested scopes within your app, but you would never "define" two scopes in the controller

it's not clear what you are trying to acheive.

can you explain on a higher level what the requirement is, and/or, provide a plunker or jsfiddle with the code in question
MrChrisDavidsAuthor Commented:
Indeed it's two variables in a scope - nothing else. Sorry about the mixup there.

I have a dynamic list of servers shown as panels on a view. There are some details on these servers that are common for certain sets and those are the ones that I'd like to render on the view

The details are however accessed from another datasource and the only matches are some IDs from the servers panel that can be matched if they're valid for detail_x kind of.

Let's say, for instance - 20 servers are used for application XYZ which is identified by ID 1. All servers come in with a variable servers.app_id = 1 and I'd then like to write out "XYZ" on the panel instead of the ID.

There's plenty of different IDs that would be replaced, not just the app and notes.

I'll have a look at a directive - see if I can get that working. Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.