How can outside code communicate with controllers?

2,570 views
Skip to first unread message

Nick R

unread,
Sep 30, 2011, 4:19:44 AM9/30/11
to ang...@googlegroups.com
Does a controller have to be a function?  How can the rest of my application get at the actual controller instance and call methods on it and such?

I have a complex game-like application (angular is not good at games, right?) and I wanted to add a little inspector panel to it that lets you edit object properties with buttons and forms (seems like a job for angular, right?).  Unfortunately, since the controller is a function, I'm not sure how my application can tell it which object to edit.  I wanted to have a set_current_object method on it that would perhaps call $eval to ensure it rendered and such.

I'm sure this question has been answered before but the best I could find was "make a service" and I'm not really sure how that solves anything.  How would the rest of my application communicate with a service?

I even tried using an intermediate object, like this (it's in coffeescript):

inspector = current_item:null
window.InspectorController = ->
  @inspector = inspector

But even if I change the value of inspector.current_item, the controller doesn't notice.  How can I get it to $eval once I've changed it?

Batman

unread,
Sep 30, 2011, 4:45:46 AM9/30/11
to AngularJS
In my understanding a controller is tightly bound to a view (or
partial i.e your html). You shouldn't call controller function from
the outside of its own view.

The easy way to share the same object between different views (html
sections) is to define your own $service which then holds YOUR
EDITABLE object and getObject, setObject methods.

Then you can inject this $service to your edit view (your inspector
panel). getObject inside the controller, change whatever you need to
change and then commit back to $serive via setObject.

The same $service could be used to to another view (your game html) to
get the modified object.

I'm not sure this is the correct way to do this but this is how I
would do it (IMO if it works, its correct).

There are discussions on this group regarding how to do the inter-
controller communication but to the best of my knowledge angular right
now doesn't define a STANDARDIZED way to do that.

florin

unread,
Sep 30, 2011, 7:27:04 AM9/30/11
to AngularJS
I consider this a hack and don't really use it. To access read/write
an angular controller from outside angular:

- somewhere outside angular, grab the html element that hosts the
controller (assumed 'body'):
var myController = angular.element('body').scope(); // <body
ng:controller="myController">...</body>
- read / write desired property:
myController.myObj.myProp = 'myNewValue';
myController.$root.$digest(); // propagates view changes

Angular way of using outside javascript resources is to wrap them.

Dan Doyon

unread,
Sep 30, 2011, 11:48:55 AM9/30/11
to ang...@googlegroups.com
Using services to communicate between controllers is the correct way.

--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to
angular+u...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/angular?hl=en.

-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1410 / Virus Database: 1520/3925 - Release Date: 09/28/11

florin

unread,
Sep 30, 2011, 8:59:17 PM9/30/11
to AngularJS
Reading back it appears I wasn't clear. I said of my suggestion that
it was a hack. I use it in firebug.
> For more options, visit this group athttp://groups.google.com/group/angular?hl=en.

danielzzz

unread,
Oct 1, 2011, 3:29:53 AM10/1/11
to AngularJS


On 30 sep, 17:48, "Dan Doyon" <dando...@yahoo.com> wrote:
> Using services to communicate between controllers is the correct way.

it would be great to have a word about it (+ some example) in the
documentation, as this is a recurrent doubt most angular newbies have
(myself included).

best,
dan

rur

unread,
Oct 1, 2011, 8:34:18 AM10/1/11
to AngularJS
My understanding of how Angular services should be used is as a bridge
from a point within Angular to another point, either inside our
outside Angular.

For example if a particular controller needs to communicate with the
following: a remote service, another controller, other javascript in
your application, the browser... then a service should be used to
encapsulate that. In the situation your describing my approach to be
to use an intermediary wrapped by an Angular service.

rur

Nicholas Retallack

unread,
Oct 3, 2011, 10:04:32 PM10/3/11
to ang...@googlegroups.com
Can you give me an example of how to use a service from outside code?  How is it going to inject the service into my outside code?  How can I use a service without injecting it?

Anyway, I decided to drop angular in favor of React for this project.  It's less intrusive.

Igor Minar

unread,
Oct 4, 2011, 7:58:53 PM10/4/11
to ang...@googlegroups.com
assuming that you are using ng:autobind and a custom service you can do this:

angular.service('myService', function() {
  //...
  return function() { alert('hello from the service') };
};


var doc = angular.element(document),
     rootScope = doc.scope(),
     myService = rootScope.$service('myService');

myService(); // alerts


I'm just not sure if this is the right way to integrate your app with angular. Typically  it's done the other way around (the service calls your non-angular code or registers callbacks with non-angular code).

/i
Reply all
Reply to author
Forward
0 new messages