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);
}
}])
-
instead you should be accessing the Student services in the activities controllerSameer Shemna– Sameer Shemna2014年12月24日 06:49:00 +00:00Commented Dec 24, 2014 at 6:49
1 Answer 1
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.
1 Comment
Explore related questions
See similar questions with these tags.