Passing arguments to controllers

5,908 views
Skip to first unread message

Izzy

unread,
May 10, 2012, 4:49:06 PM5/10/12
to AngularJS
I have a status page for a simple service running elsewhere.
The service exposes an api to check on the jobs running:
site.com/foo/inprogress - will return the in-progress jobs
site.com/foo/-10 - will return the last 10 scheduled
jobs

I have a controller like:

function FooCtrl($scope, $http) {
$http.get('site.com/foo/inprogress').success(function(data) {
$scope.foo = data;
});
}

basically, i want to be able to vary the 'inprogress' part to '-10' or
whatever

it's not immediately obvious to me via the documents how i would setup
a scope of ng-controller to have some variable argument.

help!
thanks,
-izzy
Message has been deleted

Andy Joslin

unread,
May 10, 2012, 8:30:50 PM5/10/12
to ang...@googlegroups.com
Hi Izzy,

In the most basic way, you want somehting like this: http://jsfiddle.net/V4Azc/5/

You can declare functions inside a controller and call them from the html.


Optimally, you would create a service to do all this http getting and inject it into your controller.. but the basic way is better first.

Izzy

unread,
May 10, 2012, 8:44:57 PM5/10/12
to AngularJS
Hey Andy,
Thanks for the reply.
That's close, but not exactly what i'm looking for.

Your fiddle requires some user interaction to have either the last
10 or the inprogress be loaded.

Is there no way to indicate to the controller on it's first pass
which of the two loading functions to use?
Thanks,
-Izzy

Andy Joslin

unread,
May 10, 2012, 9:02:29 PM5/10/12
to ang...@googlegroups.com
Ah, I see what you mean now.

How about just declare it in the html?

<div ng-controller="Ctrl">
{{getInProgressData()}}
</div>

<div ng-controller="Ctrl">
{{getTenLastJobsData()}}
</div>

Then just have two different functions in your scope?

Izzy

unread,
May 11, 2012, 10:07:17 AM5/11/12
to AngularJS
For a second, I thought we had the solution.

Unfortunately, calling those function in {{}} is creating an infinite
loop.

Any other ideas?
I tried to look into ngServices, but the documentation is very
confusing.

Izzy

unread,
May 11, 2012, 11:23:24 AM5/11/12
to AngularJS
after reading more of the API, i stumbled upon ngInit

i'm now doing:

<div ng-controller="Ctrl" ng-init="toLoad=-10;update();">
...
</div>

and ... ng-init="toLoad="inprogress";update()">

the ng-init only runs once, whereas {{update()}} was creating a
binding that caused infinite loops.

Unfortunately, ng-init runs _after_ the Ctrl has already been executed
on the scope, which is why i have the manual call to update() in
there.

Maybe not perfect, but I think it at least makes me happy about not
duplicating code _too_ much.

Thanks!

Dan Doyon

unread,
May 11, 2012, 11:53:42 AM5/11/12
to ang...@googlegroups.com
One thing to note, even though ng-init exists in api, it (at least in past) was not recommended to use ng-init in production code.

--dan
> --
> 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.
>

Peter Bacon Darwin

unread,
May 11, 2012, 11:57:03 AM5/11/12
to ang...@googlegroups.com

It is more common to put this kind of stuff in a controller.

...from my mobile.

Izzy

unread,
May 11, 2012, 12:00:21 PM5/11/12
to AngularJS
I would very happily put this in the controller, but how?

How can I avoid writing two almost identical controllers in this
situation?

Andy mentioned services, but the docs on services are extremely
lacking in clarity.


On May 11, 11:57 am, Peter Bacon Darwin <p...@bacondarwin.com> wrote:
> It is more common to put this kind of stuff in a controller.
>
> ...from my mobile.

Dan Doyon

unread,
May 11, 2012, 12:17:42 PM5/11/12
to ang...@googlegroups.com
how about something like this

Izzy

unread,
May 11, 2012, 12:24:12 PM5/11/12
to AngularJS
I dont see how i can use that to have two of the same controller with
a different version on the same page.

I forked your fiddle here: http://jsfiddle.net/Tr2Fn/ to show more of
the style markup i need.

to re-tie this to the original issue, i need an in-progress-jobs
section at top, and a last-n-jobs below it.

Your solution might work if they were completely separate pages,
unfortunately I dont have that benefit.

Vojta Jína

unread,
May 21, 2012, 12:46:03 AM5/21/12
to ang...@googlegroups.com
Just put the shared logic into a service and inject this service into
all controllers that require it...

V.

Izzy

unread,
May 21, 2012, 3:32:40 PM5/21/12
to ang...@googlegroups.com
The logic that modifies a controller's scope doesn't seem like it should be in a service.

Or am I missing something?
>> > > For more options, visit this group at
>> >http://groups.google.com/group/angular?hl=en.
>>
>> > --
>> > 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
>> > For more options, visit this group at
>> >http://groups.google.com/group/angular?hl=en.
>
> --
> 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+unsubscribe@googlegroups.com.

Vojta Jína

unread,
Jun 10, 2012, 3:07:18 AM6/10/12
to ang...@googlegroups.com
Well, if you need to share the modified value in between different
scopes/controllers, I would put the variable into the service and
publish this service to scope.
But this really depends on your use case, can you share simple fiddle ?

V.
>> >> > angular+u...@googlegroups.com.
>> >> > > For more options, visit this group at
>> >> >http://groups.google.com/group/angular?hl=en.
>> >>
>> >> > --
>> >> > 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.
>> >
>> > --
>> > 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.
>> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/LYGsQMVG62IJ.
>
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.

rte...@gmail.com

unread,
Jul 1, 2012, 4:48:23 PM7/1/12
to ang...@googlegroups.com
I have a controller with a configurable parameter. I would like to insert the parameter in the template itself. Is there a way to achieve this? I.e., is there a way to pass a parameter from a DOM element to its controller? 

Thanks!

dei...@gmail.com

unread,
Oct 19, 2012, 10:34:41 PM10/19/12
to ang...@googlegroups.com, rte...@gmail.com
You can retrieve some information from DOM thru $element.

2012년 7월 2일 월요일 오전 5시 48분 23초 UTC+9, rte...@gmail.com 님의 말:

ganaraj p r

unread,
Oct 20, 2012, 3:38:19 AM10/20/12
to ang...@googlegroups.com
Have a look at the ng-init tag. It allows you to do some initialization.

On Sun, Jul 1, 2012 at 9:48 PM, <rte...@gmail.com> wrote:
I have a controller with a configurable parameter. I would like to insert the parameter in the template itself. Is there a way to achieve this? I.e., is there a way to pass a parameter from a DOM element to its controller? 

Thanks!

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/6_B3oLLUZjEJ.

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.



--
Regards,
Ganaraj P R
Reply all
Reply to author
Forward
0 new messages