4

I'm assuming this is quite straightforward, but how can I have global functions that I can access in my controllers by storing the function in the '.run' phase?

Quite simply, a button is clicked in the DOM that triggers the global function that is called in the appropriate controller..

I've tried various methods and would like a really simple approach to this that helps to minimise the amount of code I've currently got. See below:

**HTML**
<input data-ng-click="clearSignInError()" data-ng-model="email" 
type="email"
id="inputEmail" class="form-control" placeholder="Email address" 
required>
**JS**
app.run(['$rootScope', '$state', function($rootScope, $state) {
 $rootScopecope.clearSignInError = function () {
 $rootScope.loginError = null;
 $rootScope.loginSuccess = null;
 };
}]);
app.controller('loginCtrl', ['$scope', 'Auth', '$state', '$rootScope'
 function($scope, Auth, $state, $rootScope) {
 $rootScope.clearSignInError()
}]);
Daniel
9,85913 gold badges54 silver badges72 bronze badges
asked Mar 2, 2016 at 12:38
3
  • 1
    you can use factory or inject controller Commented Mar 2, 2016 at 12:39
  • has been asked before stackoverflow.com/questions/35370161/… Commented Mar 2, 2016 at 12:47
  • 1
    I will check this out now! Commented Mar 2, 2016 at 12:53

2 Answers 2

2

Your best bet might be to use Services or Factories, and then inject these as dependencies when needed throughout your application.

Angular Services

Angular Factories

Edit: Added an example service in this plunkr

https://plnkr.co/edit/zqWS9gkTsm2OF1FTb24Z?p=preview

app.service('AuthService', function() {
 this.loginSuccess = function() {
 // do stuff when login is successful
 }
 this.loginError = function() {
 // handle login errors here
 }
 this.clearLoginError = function() {
 console.log("Clear Login Error");
 alert("Clear Login Error");
 }
});
// modified your controller
app.controller('loginCtrl', ['$scope', 'Auth', 'AuthService', '$state', '$rootScope', function($scope, Auth, AuthService, $state, $rootScope) {
 $scope.clearSignInError = AuthService.clearLoginError;
}]);
**HTML**
<input data-ng-click="clearSignInError()" data-ng-model="email"
type="email" id="inputEmail" class="form-control" placeholder="Email address" required>
answered Mar 2, 2016 at 12:42

13 Comments

I did try the factory option although I must have got it wrong. Based on my above code, how could i achieve what I need setting up a service? I'm simply removing an error on screen following an incorrect form submission when the user clicks back on the input box.
Just been trying the new code, although I'm getting a red line under $scope.clearSignInError() in my controller?
Sorry that line should be $scope.clearSignInError = AuthService.clearLoginError(); and the HTML should be data-ng-click="clearSignInError"
Still no luck I'm afraid, I've tried the following: 'app.service('AuthService', function() { this.clearSignInError = function($scope) { $scope.loginError = null; $scope.loginSuccess = null; }; });' and in my controller: '$scope.clearSignInError = AuthService.clearSignInError();'
I have updated my answer with a plunkr for you, so you can see it in action.
|
1

You can make use of a factory instead of hooking up with rootScope. In this way, you can include your factory as a dependency in any controller you want. Here's a video demonstration which might help you to get started:

Factories In AngularJS

answered Mar 2, 2016 at 12:46

4 Comments

If this is the case, I assume I will not need to use all of this code in the factory: $rootScopecope.clearSignInError = function () { $rootScope.loginError = null; $rootScope.loginSuccess = null; };. Instead, I would use $scope.loginError = null; $scope.loginSuccess = null; and store these in a function that is called in the controller using $scope.clearSignInError?
Yes. You can do that.
But that's not called using $scope.clearSignInError. Its called using your factory name.
I'm trying this now, but I'm also using another 'return' method in my existing factory for another function. It's saying 'unreachable code' at the start of the 'return' method for some reason - everything else seems fine but just want to check this issue out first before trying it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.