Run code once ngResource callback is completed

In my controller, I have a function for creating resources with ngResource

app.controller 'CalculationsCtrl', ($scope, Calculation)->

  $scope.save = ()->
    $scope.busy = true
    Calculation.create($scope.calculation,
      (successResult)->
        console.log ("sucess")
      , (failResult)->
        console.log ("failure")

    console.log("code after callbacks")
    $scope.busy = false

After the callbacks of .create are executed, I want to run the code below

console.log("code after callbacks")
.

I attempted using .then but it appears that ngResource does not support it.

Calculation.create(...).then is not a function
.

Is there an equivalent method to .then for ngResource?

Answer №1

When using the $resource instance (such as Calculation in this case), there is a $promise property available which allows you to use .then like so:

Calculation.create($scope.calculation).then(function (response) {
    ...
});

You can refer to the last example in the documentation for further clarification.

Answer №2

With Angular $resource, you can work with promises and utilize the .then method.

Calculation.create().$promise.then( function( result ) {
    console.log("Operation successful");
}, function( issue ) {
    console.log("Encountered error");
);

Answer №3

The error "Calculation.create(...).then is not a function." occurs when callbacks return strings instead of server responses, as seen in the <code>console.log
output.

It is essential for the callback to return the server response.

In the app controller 'CalculationsCtrl', there is a function defined with parameters ($scope, Calculation).

  $scope.save = ()->
    $scope.busy = true
    Calculation.create($scope.calculation,
      (successResult)->
        console.log ("success")
        successResult
      , (failResult)->
        console.log ("failure")
        failResult
    ).$promise.then(
       function(success){$scope...}, 
       function(fail){$scope...}
     )

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

Tips for eliminating the "#" symbol from a URL in AngularJS

My URL: eDemo/admin/test/list/#/list I wish for: eDemo/admin/test/list JavaScript: var serviceBase ="http://admin-pc/eDemo/admin/test/"; function config($routeProvider, $locationProvider) { $routeProvider .when('/add', { ...

What caused the failure of the ++ operator in production mode?

Encountering an issue with a reducer in my code. There is a button with an onClick function that triggers a NextBox action to alter the state of a React UseContext. The application was built using Vite, and while running npm run dev, everything functions a ...

The console correctly detects the value, but is unable to set the innerHTML property of null

I am currently working on a webpage that allows users to sign in and create an account. The issue I'm facing is that when I try to display the user's information, I encounter the error 'Cannot set property 'innerHTML' of null.&apos ...

Keep track of the user's email address as they complete the form

I currently use a Google Form to gather information from employees who work in remote locations Emp No * Punch * Customer details / mode or travel All the data collected is stored in a Google spreadsheet structured as follows: Timestamp Emp No Punch ...

Receiving Accurate JSON Data in PHP

As a newcomer to PHP, I have studied some examples and written a code to fetch data from a database. However, when the database is not found, I receive a text response. Can anyone advise on how to return a proper JSON response if the database is not found ...

Having difficulties persisting information from freshly generated input fields with the "Add" button to the database

Currently, the issue lies with the database only accepting data from the first input field in PHP. <form class = "spendings" method ="POST" action="InsertToDatabase.php"> <div id="numbering">ITEM 1: </div> <div class="entry"> ...

Guide on fixing a parent div's position for a specific duration using the scrollorama plugin

Currently, I am utilizing the "scrollorama" plugin to create a parallax effect on my website. I have a specific requirement where I need to fix the parent div of the animated elements for a certain duration and then release it to allow the next section of ...

Obtaining the offspring of a component utilizing an attribute guidance

I'm working on a directive that is restricted to an attribute. My goal is to achieve one of two things: If a specific condition is satisfied, I want to use the children within the element containing the directive as the model (which represents the con ...

Tips for positioning a modal in the center specifically for zoomed-in mobile devices

I created a modal that uses the following function to center itself: center: function() { var top=Math.max($window.height() - $modal.outerHeight(),0) / 2; var left=Math.max($window.width() - $modal.outerWidth(),0) / 2; $modal.css({ ...

Display a still image initially, then switch it out with an animated GIF once the GIF has finished loading

I am in the process of creating a website that features numerous large (800KB) animated GIFs. To optimize the initial loading speed, I have decided to display the first frame of each GIF first and then replace it with the full animation once it has been fu ...

Is there a way to transform individual data into a collective group dataset?

Here is the input data provided: data = [ { name: "John Cena", groupName: "WWE" }, { name: "Nandini", groupName: null }, { name: "Rock", groupName: "WWE" }, { name: "Vinay", groupName: null }, { name: "Rey Mesterio", groupName: "WWE" ...

Having trouble with installing NPM and ng commands, neither of them is working. I may have to uninstall everything and begin from scratch

Learning Angular and how to use the terminal is all new to me. I recently installed NPM and attempted to use ng serve, but encountered a series of issues. At this point, I am considering completely uninstalling everything so I can start fresh. I've d ...

Avoiding the impact of internal iframe positional shifts on browser history

Note: While many questions involve users writing a new location into an iframe from the outside and being advised to use location.replace, this is not my situation. Update: I have included my original description below, but I wanted to rephrase the issue ...

How to rotate a SVG transformation matrix around its center point

CSS .square { background-color: green; height: 40px; width: 40px; } JS var square = { sizeReal : { "width": 40, "height": 40 } , position : { "x": 100, "y": 100 } }; $(". ...

I am looking to include an SVG icon, like a separate React component, within the "title" parameter of a React-Bootstrap Drop Down

Looking to create a header with the Profile icon and Loggedin user positioned on the right side of the Bootstrap Navbar. My goal is to achieve a UI similar to the image linked below without using the Dropdown component. In the image, there are two parts ...

I'm looking for a solution to pass a PHP object as a parameter in JavaScript within an HTML environment,

I am currently working on a project using Laravel 5. I have a table in my view, and I want to pass all the data values to a JavaScript function when a link is clicked. I have tried several methods but have been unsuccessful so far. @foreach ($basl_offic ...

Unique tooltip when hovering over a div element and an image contained within the div

How can I display two different tooltips when mouseover a div and an image inside the div? <div id="_div1" runat="server"> <ul> <li class="right"> <asp:Image ID="img1" runat="server" /> </li> </ul> </di ...

PugJS is failing to properly interpolate numbers from the JSON file

My goal is to display a list of interfaces retrieved from my router API, which is in JSON format. However, when using Pug, only the strings are being rendered and not the numbers. Here is the output (it displays correctly in HTML): em1 192.168.0.0/24 Addr ...

What is the process for creating two columns with an input box beneath them?

I am facing a challenge with my code. I am struggling to create the desired design where there are two columns and below them an input box that will be displayed when a button is pressed. The design I am aiming for can be viewed here: enter image descripti ...

Updating Mapped Components with Selected State

One of the components in my project is a mapped component that dynamically displays API data. Each card displayed by this component receives unique props, resulting in cards that look different from one another. An example can be seen below. View Example ...