Having trouble reaching an injected dependency beyond the controller method

Can an injected dependency on a controller be accessed outside of it?

function clientCreateController(ClientsService, retrieveAddress) {
  var vm = this;

  vm.searchCep = searchCep;
}


function searchCep(cep) {
  retrieveAddress.find(cep)
    .success(function(data) {
      parseAddress(data).bind(this);
    })
    .error(function(err) {
      // showAlertDanger(vm, 'Invalid CEP.');
      console.log(err);
    });
}

I am calling the method from a click event on a button.

Thank you!

Answer №1

Attempting to access the parameter recuperarEndereco outside of the function pesquisarCep is not possible. This is because within the execution context of the pesquisarCep function, the variable recuperarEndereco has not been declared. An example of this can be seen in this JSFiddle: http://jsfiddle.net/dLhmozf3/. It throws an error:

function outsite () {
    console.log('param: ' + param);
}

var f = function (param) {
    var me = outsite;
    me();
};

f();

To successfully utilize the outside function, it must be defined as follows:

function pesquisarCep(cep, recuperarEndereco) { ...
. You should then call the function like this:
pesquisarCep(cep, recuperarEndereco)
.

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

Creating a basic popup with jQuery: A step-by-step guide

Currently in the process of creating a webpage. Wondering how to create a popup window with an email label and text box when clicking on the mail div? ...

Ways to manually change the history in angular.js

Currently, I am facing a challenge in removing the querystring (specifically an invitation token) from the URL without triggering a page redirect. Here is an example of what the URL looks like: example.com/?invitation=fooo In our project, we are using n ...

"Disabling Click Event on Sidebar Menu in Angular: A Step-by-Step Guide

I am working on an Angular project with a sidebar that I want to modify in order to disable click events on the menu items. My goal is to guide users through a specific flow without allowing them to navigate freely. However, simply disabling the [routerLin ...

Is there a way for me to identify when I am "traveling" along the same path?

I want to create a toggle effect where a view is hidden if the user tries to revisit it. This can be useful for showing/hiding modal boxes. Here's the code I attempted: /* Root Instance */ const app = new Vue({ router, watch: { '$route&a ...

Trouble with linkage in jQuery for AJAX requests in an external .js document

My asp.net application has a master file that includes the jquery.js file. On another page that uses this master page, I have my own jquery code. I attempted to move this code to an external file and then include it in my .aspx file. While most functions ...

unable to display preview images using the youtubev3 API

Currently in the process of creating a YouTube clone using the YouTube V3 API import React from 'react' import { Link } from 'react-router-dom'; import { Typography, Card, CardContent, CardMedia } from '@mui/material'; import{ ...

Surprising outcomes when using Mongoose

Questioning Unusual Behavior There is a model in question: //test.js var mongoose = require('../utils/mongoose'); var schema1 = new mongoose.Schema({ name: String }) var schema2 = new mongoose.Schema({ objectsArray: [schema1] }); schema1.pre( ...

Transferring a PHP session variable to JavaScript in a separate file

I successfully imported data from a CSV file into PHP and organized it into a nested array. To ensure the data is easily accessible across different files, I stored the array as a session variable. Now, the challenge lies in accessing this session variable ...

Removing a dynamic component in Angular

Utilizing Angular dynamic components, I have successfully implemented a system to display toaster notifications through the creation of dynamic components. To achieve this, I have utilized the following: - ComponentFactoryResolve - EmbeddedViewRef - Ap ...

Ways to retrieve information from a $$state object

Everytime I try to access $scope.packgs, it shows as a $$state object instead of the array of objects that I'm expecting. When I console log the response, it displays the correct data. What am I doing wrong? This is my controller: routerApp.controll ...

What is the best way to encode only a specific section of a JavaScript object into JSON format?

Currently, I am in the process of developing a 2D gravity simulation game and I am faced with the challenge of implementing save/load functionality. The game involves storing all current planets in an array format. Each planet is depicted by a Body object ...

The operation was computed twice

Below is an example: test.html <!DOCTYPE html> <html ng-app ng-controller="AppController"> <head> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="script1 ...

Is it advisable to perform several Firestore queries within a single cloud function to minimize round-trip times?

Exploration Within my application scenario, I have a specific screen that displays 8 different lists of items. Each list requires a separate query to Firestore, running asynchronously to retrieve documents from various collections. Through profiling the e ...

Using Jquery to trim the data returned from the server response

Utilizing asp.net for obtaining the server response via JQuery AJAX in JSON format has been my approach. I've experimented with both JQuery.getJSON() and typical jQuery response methods, followed by conversion to JSON format using $.parseJSON. Howeve ...

Combine a JSON object and a JSON array by matching the value of the JSON object to the key of the JSON array

I am looking to create a JSON array in node by combining two JSON objects and arrays. `var template = { "key1": "value1", "key3": "value3", "key4": "value3", "key6": "Dummy Value1" }; var data = [ { "value1": "1", "value2": "2", ...

Automatically submitting a form in React.js based on certain conditions being met

Does anyone have experience with React login and register form buttons that trigger Redux actions? I'm facing an issue where both actions are being dispatched at the same time when certain conditions are met. Here is my code snippet: const LoginPage ...

Enhance the functionality of jQuery sortable by including additional details

I have a li list that I have implemented sortable functionality using jQuery. In order to ensure that the updated data is sent to the correct destination, I need to include some hidden values in the serialized data. How can I achieve this? HTML <ul i ...

An issue has occurred: TypeError - It is impossible to access the 'forEach' property of an undefined object

Having trouble with a promise issue that I just can't seem to solve. Whenever I enter 'pizza' into the search bar and click search, the console displays an error message: TypeError: Cannot read property 'forEach' of undefined I&ap ...

Conceal a division on a webpage according to the URL of the specific

<script> function hideSearchField() { if (/menu/.test(window.location.href)) { document.getElementById('searchfield').style.display = 'none'; } } hideSearchField(); </script> I am trying to accomplish this using Jav ...

Can someone explain how to utilize the setAttribute() method in JavaScript to create an ng-click attribute which triggers a specific function in AngularJS?

I am trying to use querySelector to select a button and set an attribute of "ng-click=doSomething()" Even after attempting to select the button and then using setAttribute("ng-click", "doSomething()"), I'm still encountering issues This is my DOM st ...