What is the process for invoking a function within a dependency module?

Currently, I am diving into the world of Angular and have encountered an issue. How can I successfully call a method from a dependency module in Angular?

app.js

(function(){
    var myApp = angular.module('myApp',['map']);
    myApp.filter('move', function(){
        return function(name){
            return walk(name);
        } 
    });
})();

map.js

(function(){
    var mapMod = angular.module('map',[]);

    mapMod.factory('walk', function(){
        return "walking...";
    });

})();

After adding app.js and map.js to the HTML page's scripts,

 ReferenceError: walk is not defined

I am determined to find a way to properly call the 'walk' method within the 'move' filter. Any suggestions?

Answer №1

Well done, you're very close to getting it right! Just remember to include walk in the filter and choose your preferred method of dependency injection, whether it's through explicit annotation using array syntax (as shown below), $inject syntax, or implicit dependency annotation with ng-annotate for safe minification. By listing map as a dependency for your module myApp, all the components registered in the map module will also be accessible in myApp. Additionally, make sure to consult the documentation on how to write a factory/service.

(function() {
  var mapMod = angular.module('map', []);

  mapMod.factory('walk', function() {
    return function(name) {
      return "walking..." + name;
    }
  });

})();
(function() {
  var myApp = angular.module('myApp', ['map']);
  myApp.filter('move', ['walk',
    function(walk) {
      return function(name) {
        return walk(name);
      }
    }
  ]);
})();

(function() {
  var mapMod = angular.module('map', []);

  mapMod.factory('walk', function() {
    return function(name) {
      return "walking..." + name;
    }
  });

})();
(function() {
  var myApp = angular.module('myApp', ['map']);
  myApp.filter('move', ['walk',
    function(walk) {
      return function(name) {
        return walk(name);
      }
    }
  ]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div>{{"test"|move}}</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Are there any jQuery Context Menu plugins clever enough to handle window borders seamlessly?

After reviewing UIkit, as well as some other jQuery Context Menu plugins, I have noticed that they all tend to exhibit a similar behavior: The actual menu div renders outside the window, causing valuable content to be hidden from view. Is there a way to ...

Transparent background with opaque text

I noticed that my div has a gray background with some opacity. <div className="absolute w-full h-full top-0 left-0 right-0 bottom-0 bg-slate-500 opacity-50 z-10"> <p className="relative top-1/2 px-8 text-center hea ...

Utilizing HTML properties in stenciljs for variable binding

I'm struggling to display a value with custom HTML inside an element. example: this.title = 'Hello <b> stencil </b>'; << response value from an API Binding: <h1>{this.title}</h1> I hope to achieve similar ...

Display a React constant value inside an alert message box

Looking to create a pop-up alert box with a random number? Start by setting a constant value using the const keyword followed by the formula Math.round(Math.random() * 10000000). Next, you can access this random number in your alert box through a function ...

``What is the mechanism behind callbacks in AngularJS when making a call to a REST service?

Currently, I am diving into the world of AngularJS and REST. The code snippet that I'm analyzing has the term callback repeatedly used in an authentication function. I'm curious to know if "callback" is a JavaScript or Angular keyword, or simply ...

Updating the texture passed to a RawShaderMaterial uniform in Three.js after the initial rendering process

My Three.js scene uses a canvas as a uniform for a RawShaderMaterial. Once the scene is initially rendered, I modify the canvas by painting it red. Even though I set .needsUpdate = true; for the shaderMaterial, the points do not change color. If I move th ...

Learn the method to duplicate Outer HTML with the use of jQuery or Pure JavaScript

I have successfully copied the value of an input into the clipboard using the first part of the solution provided. However, I am now trying to figure out how to copy HTML content like an entire <p> tag. Currently, when attempting this, I am encounter ...

The addEventListener method fails to respond to changes in input

Can someone assist me with this issue? I am facing a problem where the input.addeventlistener is not detecting files on change. My setup involves using Vue with Rails. I am looking to trigger the event listener in the mount hook of a Vue component. mo ...

Incorporate additional query parameters into a dynamic route with React Router to enhance functionality

I am currently working on incorporating an optional query parameter to the end of a path in order to create URLs like this: "/user/1/cars?makeYear=2020" or "/user/1/cars". The relevant Route is defined as shown below. I have been having ...

Employing a combination of IF clauses for form validation

While implementing form validation, I encountered an issue where only the length check was being performed and not the empty string check. This led to only one error message being displayed instead of two separate messages for each scenario. How can I modi ...

Replacing a string in a textarea using Jquery

Trying to dynamically replace a string value in a textarea while typing into a textbox using jQuery. Utilizing the keypress event for this functionality. Any ideas on what could be causing issues with this example? <input type="text" id="textbox" /> ...

Calculate the difference in time spans using Javascript

If I have a time range {"start_time":"8:30", "end_time":"18:00"}, and I want to subtract another time range like {"start_time":"12:30", "end_time":"14:00"}, the result would be: [{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time ...

Implementing validation and displaying fields with v-model in vue.js

Looking at this code snippet: <button type="button @click="editing=true">Edit</button> <form v-show="editing" @submit="onSubmit"> <textarea v-model="text"></textarea> </form> <div> Current value: {{text}} </ ...

The search feature is currently malfunctioning due to an issue with the array in Vue and Vuetify's v-data-table component

Issues with searching by phone array. While creating a table on Vuetify, I encountered an issue - the search functionality does not work when trying to search by an array. I am struggling to find a solution to this problem. Any assistance would be greatly ...

Angular - Modify column based on selection in dropdown menu

I have a situation where I need to dynamically change the values in a column of my table based on the header that is selected from a dropdown menu. Within my controller, I defined the following: vm.pricing = [ { id: 'price1' ...

I have decided to forego the method I initially wanted to use

click here for image description I am using a method that is successfully performing its function, as I can see the URL in the console output. However, I am puzzled by why the method name appears scratched out. Despite this visual cue, the method is funct ...

Steps to refresh the data source upon clicking a checkbox row in a Kendo grid view:

Is it possible to change data in a row without requiring an update button when a checkbox is checked or clicked on? There is sample code available for this functionality in Kendo grid view. However, I am experiencing an issue where the data from the dataso ...

Unlocking Node.js packages within React JS is a seamless process

Currently, I am developing a React Serverless App with AWS. I am looking for ways to incorporate a Node JS specific package into the React JS code without requiring Node JS on the backend. One package that I need access to is font-list, which enables list ...

AJAX cached outcomes

Trying to educate myself on AJAX using w3schools.com, but struggling with a particular example: xhttp.open("GET", "demo_get.asp", true); xhttp.send(); In the above example, there might be a cached result. To prevent this, you can include a unique ID in t ...

Exploring AngularJS: Effortlessly Retrieving Object Values

HTML: <div ng-app = "" ng-controller = "personController"> <p>Persoon: <select ng-model="persoon"> <option ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option> & ...