Take a look at the answer for this on stackoverflow for some guidance.  In 
my situation I put the shared functionality into a Factory.  You'll find an 
example of that as well as using the $rootScope to do it.

http://stackoverflow.com/questions/15025979/can-i-make-a-function-available-in-every-controller-in-angular



On Tuesday, February 3, 2015 at 10:31:34 AM UTC-6, Satish B wrote:
>
> Please let me know the answer for this. I'm trying my best to rewrite the 
> code to suite the style-guide. But need answer for this for now. I'll be a 
> great help. Thanks ..
>
>
> Here is my sample code:
> ------------------------------
>
> ---------------------------------------------------------------------------------------
> 'use strict';
>
> myApp.controller('First', ['$scope', 'Users', 'Assign', function($scope, 
> Users, Assign){
>         Users.users(function(results){
>             $scope.users = results;
>         });
>
>     $scope.fn = function(id, n1){
>         $scope.n1 = n1;
>         $scope.id = id;
>     }
>
>     $scope.ln = function(){
>         Assign.assign(function(results){
>             $scope.work = results;
>         });
>     }
> }]).controller('Second', ['$scope', 'Users', 'Lang', function($scope, 
> Users, Lang){
>         Lang.langs(function(results){
>             $scope.lang    = results;   
>         });
>         Users.users(function(results){
>             $scope.users = results;
>         });
>     
>     $scope.fn = function(id, n1){
>         $scope.n1 = n1;
>         $scope.id = id;
>     }
> }]);
>
>
> ---------------------------------------------------------------------------------------------------------------------
>
>
>
> I changed above code to:
>
>
> ---------------------------------------------------------------------------------------------------------------------
>
> (function() {
>
>
> 'use strict';
>
> myApp.controller('First', ['$scope', 'Assign', 'separate', 
> function($scope, Assign, separate){
>        
>     $scope.ln = function(){
>         Assign.assign(function(results){
>             $scope.work = results;
>         });
>     }
> }]).controller('Second', ['$scope', 'Lang', 'separate', 
> function($scope,Lang, separate){
>         Lang.langs(function(results){
>             $scope.lang    = results;   
>         });
> }]);
>
> function separate($scope) {
>      Users.users(function(results){
>             $scope.users = results;
>         });
>
>     $scope.fn = function(id, n1){
>         $scope.n1 = n1;
>         $scope.id = id;
>     }
>
> }
>
>
> })();
>
> ---------------------------------------------------------------------------------------------------------------------
>
> I got errors with above code. I played around to make global scope. 
> assigning *this *variable to a local variable etc. But couldn't make it 
> work.
> Any input on solving above problem will be very helpful.
>
> Thanks @Sander Elias and @Nicholas Smith
>
> *[*Am reading style-guide and trying my best to understand it.*]*
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to