Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to create a global or from scope javascript function ? #1108

Closed
yaronmil opened this issue Nov 13, 2024 · 3 comments
Closed
Assignees
Labels

Comments

@yaronmil
Copy link

I want to reuse a calculate value javascript functin .

I want to be able to call the same function from differant component.

how can it be done ?
is it possible to do so without using the global window objec ?

@yash-sharma-js
Copy link

you can create a function inside a service and use it anywhere in your application by injecting that service. Angular services are designed to share functionality and data across components, directives, or other services.

@lane-formio
Copy link
Contributor

Agent Jeriah Henley linked Freshdesk ticket 53986 for this issue.

@brendanbond
Copy link
Contributor

Yeah, the only way you're going to be able to "reuse" a function within the form's evaluation context (so, within a calculated value for example) is either by attaching it to the global scope or by injecting it into your form's evaluation context by using the form's module, e.g.

myForm.module = {
  options: {
    form: {
      evalContext: {
        validatePhone: function(input) {
          return input.match(/^[2-9]\d{2}-\d{3}-\d{4}$/);
        }
      }
    }
  }
}

(this way you could reuse the validatePhone function across a number of forms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants