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

Encountering a Node.js error when executing the JavaScript file

When I try to run my Node.js application using the command "node main.js" in the terminal, I encounter an error saying "Error: Cannot find module 'D:\nodeP\main.js'". This is confusing for me as I have set the environment variable path ...

Ways to condense a text by using dots in the middle portion of it

How can I dynamically shorten the text within a container that varies in width? The goal is to replace the strings between the first and last words with dots so that it fits within the container. For example, Sydney - ... - Quito. It should only replace wh ...

Utilizing Vue JS to showcase a pop-up block when hovering over a specific image

There are four images displayed, and when you hover over each one, different content appears. For example, hovering over the first image reveals a green block, while hovering over the second image reveals a blue block. However, the current logic in place i ...

What exactly is the purpose of the QueryString function and how does it work

I recently took on the role of editor for a website that I did not create. One of the webpages contains a map feature, and I've been tasked with changing how the map loads initially on the webpage. As I review the JavaScript code, I'm unsure if ...

Windowing box inside a container box

I am looking to implement scrolling for just the "chat-messages" div within the "chat-bar" div on my site. I want only this specific section to be scrollable, while the rest of the site remains stationary. Currently, I have to scroll through the entire pag ...

A Guide to Importing CSV Data Into a Datatable

Is there a way to efficiently import data from a CSV file and display it in a table using the datatables plugin? Currently, I have a fixed table structure: <table id="myTable" class="table table-striped" > <thead> ...

What is preventing obj from being iterable?

When I try to compile this code, an error appears stating that the object is not iterable. Why is this happening? My goal is to determine the number of users currently online. let users = { Alan: { age: 27, online: false }, Jeff: { age ...

What is the best way to implement ES2023 functionalities in TypeScript?

I'm facing an issue while trying to utilize the ES2023 toReversed() method in TypeScript within my Next.js project. When building, I encounter the following error: Type error: Property 'toReversed' does not exist on type 'Job[]'. ...

Invoking middleware within another middleware

I was recently following along with a web development tutorial on YouTube where the instructor introduced two middlewares called verifyToken and verifyUser. `verifyToken` const verifyToken = (req, res, next) => { const token = req.cookies.access_token ...

Manipulate the ng-model value within a complex array structure

Currently, I am involved in a project where I am adding dynamic form attributes to the parent element. Here is an example of what the data looks like: $scope.uploadedFiles = [{ name: sample.jpg, attr: [{ id: "caption", display: "Titl ...

What are the steps to restrict the scope of a <style> declaration in HTML?

Currently, I am in the process of creating a user interface for a webmail. Whenever I receive an email, I extract its content and place it within a <div> element for display purposes. However, one challenge that I am facing is that each email contain ...

Map on leaflet not showing up

I followed the tutorial at http://leafletjs.com/examples/quick-start/ as instructed. After downloading the css and js files to my local directory, I expected to see a map but all I get is a gray background. Can anyone advise me on what might be missing? T ...

Utilizing Angular JS and Web.API to create an Innovative File Management System

Within my setup, I have two projects. The first is a WebAPI project where I handle classes: FileClass for managing files, DirectoryClass for managing directories, and ClassContext that holds lists of both files and directories. In the HomeController, I&apo ...

Successive, Interrelated Delayed Invocations

There are two functions in my code, getStudentById(studentId) and getBookTitleById(bookId), which retrieve data through ajax calls. My ultimate goal is to use Deferreds in the following sequence: Retrieve the Student object, Then fetch the Book Title bas ...

Excluding node modules when not included in tsconfig

Within my Angular project, there is a single tsconfig file that stands alone without extending any other tsconfigs or including any additional properties. Towards the end of the file, we have the following snippet: "angularCompilerOptions": { ...

ng-model in html input with a name of "foo[]"

Apologies for the lack of a more specific title. In HTML, when we need multiple values for a given name, we utilize the name="foo[]" attribute. Upon posting the data, it arrives as an array. I am seeking this same functionality with ng-model in Angular. ...

Ways to move down only one level of an element's children, excluding sub-levels

When implementing this code snippet: jQuery: $(this).next().slideDown() with a selector :$(this).next() HTML: <li class="sub-menu two-level-collapse"> <a href="javascript:void(0);" class="two-level-collapse parent">< ...

Generate dynamic DIV elements and populate them with content

Can you assist me in getting this code to function properly? My goal is to dynamically create elements inside a div and fill each element with a value fetched from the database through a server-side function. I'm unsure if there's a better approa ...

Preventing the submission of form post values by using jQuery remote validation

     Within my form, I have incorporated two submit buttons (save & exit, next) and implemented remote email address duplication checks. Everything is functioning properly, however, upon submission of the form, I am unable to determine which specific s ...

javascript issue with onchange query

The JavaScript snippet below is included in the head section of my file. <?php echo "<script language='JavaScript'>\n"; echo "var times = new Array();\n"; echo "times[0] = 0;\n"; foreach($times as $time) { echo "times[". ...