how to change scope in 1 controller via another controller?

10926 views
Skip to first unread message

christop...@gmail.com

unread,
Jun 16, 2012, 11:53:22 AM6/16/12
to ang...@googlegroups.com
Hi,

I am relatively new to angularjs and I am looking for a way to change scope in other controllers

function ctrl1($scope){
 $scope.tochange = '';
}

function ctrl2($scope){
here I want to change $scope.tochange
}

any help would be greatly appreciated

thnx

christop...@gmail.com

unread,
Jun 16, 2012, 11:47:28 PM6/16/12
to ang...@googlegroups.com
thanks :)

Noong Linggo, Hunyo 17 2012 04:36:19 UTC+8, si Johan ay sumulat:


Lucas has a nice video (his site is popping a malware warning in Chrome at present) but here is the YouTube link


based on video this fiddle

Johan Steenkamp

unread,
Jun 18, 2012, 4:50:33 PM6/18/12
to ang...@googlegroups.com
Thanks Andy - good suggestion.


On Tue, Jun 19, 2012 at 8:33 AM, Andy Joslin <andyt...@gmail.com> wrote:
> I would use a service instead of root scope, to keep things more modular.
>
>> $rootScope.object = { value: 1 }
>> ...
>> $rootScope.object.value = 1;
>
>
> versus
>
>> Service.object = { value: 1 }
>>
>>  ...
>>
>> Service.object.value = 1;
>
> --
> 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/-/GaIHBbm4DgIJ.
>
> 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.



--
Johan Steenkamp
jo...@orbital.co.nz
+64 03 359 4290
http://www.orbital.co.nz
Message has been deleted

Johan Steenkamp

unread,
Jun 20, 2012, 4:10:15 PM6/20/12
to ang...@googlegroups.com
Lucas - I use $watch in that way. As an example I need to switch the
layout of UI based on widow width so in my general app controller (not
tied to a view) I have have a function checks window.resize event and
sets $scope.windowWidth. I then use $scope.$watch('windowWidth', ...)
in the views that need to adjust (that is just done by CSS).

I did not think of same approach for cross controller communication in
general - I guess concerned about too many watchers but not for any
validated reason.

So is this a 4th approach to cross controller communication aka
"wiring up the app":

1. Shared service
2. Events
3. Prudent pragmatic use of $root scope
4. Watchers

And a 5th - scope prototypical inheritance?
http://docs.angularjs.org/guide/scope

Johan

On Tue, Jun 19, 2012 at 10:23 AM, Lukas Ruebbelke <sim...@gmail.com> wrote:
> I definitely recommend using a shared service to communicate between
> controllers.
>
> If you look at my fiddle, I use $rootScope as an event bus just to broadcast
> an event so that the other controllers know that something has changed.
>
> Although! I was told you could do that in the controller itself via $watch.
> I need to look into that...
>
> Also, I am injecting $rootScope and not breaking law of demeter by reaching
> too far into $scope to get it ie: $scope.$root.someValue.
> --
> 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/-/xV721TpN7uEJ.

Victor Kurauchi

unread,
Nov 14, 2013, 6:30:26 AM11/14/13
to ang...@googlegroups.com, christop...@gmail.com
@Johan, thanks for sharing the fiddle. it's well explained.
Reply all
Reply to author
Forward
0 new messages