1

i am writing a code in angularjs which loads the controller file as per session demands i have a separate controller files for admin(let say ctrl1) and for staffs (ctrl2) and one common(ctrlComm) file which can be use any among them,now i want to call function of ctrl1 inside ctrlComm, i do not want to write or paste the file code again,how can i do this.Here is my code.

ctrl1:

customerSupport.controller('studentSheet',['$scope',function($scope){

$scope.searchStudent=function(studentid){
        angService.getfromRemote('students/'+studentid)
            .success(function(response){
                if(response.success){
                        ...
                 }
           })
        }
    }])

ctrlComm

activities.controller('usersActivites',['$scope',function(){

$scope.studentdetail=function(studentid){
    console.log("how can i call the searchStudent if ctrl1 here ??");

    $scope.searchStudent(studentid);

   }


}])
1
  • instead you should be accessing the Student services in the activities controller Commented Dec 24, 2014 at 6:49

1 Answer 1

3

Sharing a function is a clear sign you need a service.

Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.

I believe you already have angService in place for this. Inject this service in usersActivites and you are good to go. You can tweak your service to get student details directly. Create some function there like angService.searchStudent and from service return appropriate response.

Sign up to request clarification or add additional context in comments.

1 Comment

thaks it works, what i need to do now is,write my controller search function inside service then it will work every where. thanx once again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.