简体   繁体   中英

Calling helper function in controller

I have a function which i am using across the application in my controllers.

function hideErrorMessage() {
    var error = {};
    error.value = false;
    error.errorMessage = "";
    $scope.error = error;
}

How could i put this in a separate file and reference it across the application from there?

Below is a simple service i have created.

myApp.service('utilService', function() {
    this.hideErrorMessage= function() {
        var error = {};
        error.value = false;
        error.errorMessage = "";
        return error;
    };
});

My worry is i am using this helper function in many places, if i return an error object and then assign it to my $scope.error = utilService.hideErrorMessage() in my controller, will it be a costly operation.

I am using the hideErrorMessage() in multiple places in a same file to hide error message when it passes.

Just add a script tag with your file containing your service to the html that has your ng-app directive. Be sure to add it after the script tag for the angular library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM