Accessing template attribute value in AngularJS controllerIn AngularJS, retrieving the

I am new to using angularjs version 1.6.4 and I have implemented the module leon/angular-upload for file upload functionality in minify version. After a successful upload, the server returns a JSON object containing information about the uploaded file in the onSuccess(response) function, which is then handled in my user-registration.template.html file. Now, I need to pass this JSON object to my controller in order to save this information to my database. Below are excerpts from my code.

user-registration.template.html:

<form role="form">
                <div class="form-group float-label-control">
                    <label>Name</label>
                    <input type="text" placeholder="Name" class="form-control" ng-model="model.user.name" />
                </div>
                <!--  leon/angular-upload -->
                <div upload-button
                     url="/user_uploads"
                     on-success="onSuccess(response)"
                     on-error="onError(response)">Upload</div>

                <div class="text-center">
                    <button type="button" class="btn btn-default" ng-click="model.save(model.user)">Save</button>
                </div>
            </form>

My component "user-registration.component.js":

(function(){
    "use strict";

    var module = angular.module(__appName);
function saveUser(user, $http){
        var url = user.id > 0 ? __apiRoot + "/users/" + user.id : __apiRoot + "/users";
        var dataObj = {
            payload: JSON.stringify(user),
            _method: "PUT"
        }
        return $http.post(url, dataObj);
    }

    function controller($http){
        var model = this;
        model.user = null;


        model.save = function(user){
            console.log(JSON.stringify(user));
            saveUser(user, $http).then(function(response){
                alert(response.data.msg);
            });
        }
    }

    module.component("userRegistration", {
        templateUrl: "components/user-registration/user-registration.template.html",
        bindings: {
            value: "<"
        },
        controllerAs: "model",
        controller: ["$http", controller]
    });

}());

Answer №1

Consider storing your server response data in the $rootScope model, for example:

$rootScope.serverResponse = response;

By using rootScope, you can easily share variable data between controllers.

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

Implementing complex routing with Express.js on top of Node.js

Recently delving into the world of javascript, I have embarked on creating a RESTful API using Node.js and Express.js Here is the breakdown of my directory structure: /server.js /api/api.js /api/location/location.js My goal is to make the API modular, ...

Can you please guide me on how to convert pug (jade) to html using npm scripts?

Struggling to construct my package.json file, I find myself facing a challenge when it comes to writing scripts. "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build-css":"node-sass --output-style compressed -o bu ...

Completely enlarge this inline-block CSS div scan-line

I am looking to create a unique scan line effect that gradually reveals text from left to right, mimicking the appearance of a cathode-ray burning text into a screen's phosphors. The concept involves sliding across black rows with transparent tips. Y ...

Refreshing a data object that is shared among Vue components

I've recently started diving into Vue, and I've found myself responsible for tweaking an existing codebase. There's this data.js file that caught my attention, containing a handful of objects holding city information, like: export default { ...

"Efficiently sharing information in a multi-tenant application: strategies for seamless data transfer between front

In my development of a multi tenancy application using Node.js/Mongoose and React, I made the decision to utilize a single database for all users. The main collection, dubbed companies, serves as storage for basic company data and includes a unique compan ...

Assigning a unique identifier to each table row when iterating through a collection using

I have successfully implemented code for generating a table dynamically with angularjs: <div class="col-lg-10 col-sm-10 col-xs-10"> <table class="table table-hover"> <thead> <tr> <th>item</th> <th> ...

Guide on making an SVG tooltip using the Menucool JS extension

I am trying to add a simple tooltip to an SVG "map" when the mouse hovers over a rectangle. To create the tooltip, I would like to utilize this specific plugin. Here is how I have connected the SVG to HTML: <object data="map.svg" type="image/svg+xml" ...

Firebase 3 authentication does not maintain persistent login sessions

I recently upgraded my AngularJS app to use Firebase 3.x and AngularFire 2.x for email and password authentication. I referred to the following documentation for migration: https://firebase.google.com/support/guides/firebase-web https://github.com/fi ...

Ways to update a component's state by clicking a button located on a different component

Seeking help with changing a parent state value when clicking a button. Although there are numerous similar queries out there, I'm really struggling to grasp the concept. I've attempted lifting the state up but haven't quite nailed it yet. ...

Crushing jQuery's Sortable/Droppable

Having a little issue here. I want to be able to toggle the sortable plugin's behavior by clicking a button - basically switching between sort mode and view mode. I've attempted to achieve this with the following code: function enterSortMode(){ ...

The ability to retrieve variables within a certain scope from a function that is declared externally

Is there a method to access and print the value of a closure variable, temp, from a function defined outside the closure but referenced within it without passing temp as an argument to the function? var funcA, funcB; funcA = function () { console.log( ...

Incorporating a swisstopo map from an external source into an Angular

I am looking to integrate a swisstopo map into my angular 8 app. As I am new to angular, I am unsure how to include this example in my component: I have tried adding the script link to my index.html file and it loads successfully. However, I am confused a ...

Does catching errors cause the for loop to stop in JavaScript?

for (let index = 0, total = IDs.length; index < total; index++) { item = IDs[index]; hasError = false; try{ let id = db.getSiblingDB("door").jhi_user.findOne({"_id" : item}); } catch (error){ failedIDs.push(item); ...

Adding a new column to a table that includes a span element within the td element

I am attempting to add a table column to a table row using the code below: var row2 = $("<tr class='header' />").attr("id", "SiteRow"); row2.append($("<td id='FirstRowSite'><span><img id='Plus' s ...

Exploring Nested JSON Objects with React and Axios

I am exploring the CryptoCompare API to retrieve cryptocurrency data. As a beginner with React, I have successfully used Axios for AJAX requests. However, I am facing challenges in accessing specific JSON elements. To view the JSON data structure, you can ...

Navigating smoothly through different URLs to a single state using Angular's UI-Router

I recently started using angular's ui-router and I am looking for a way to configure multiple URLs to point to the same state. For instance: /orgs/12354/overview // should show the same content as /org/overview Currently, my $state provider is set u ...

ESLint is parsing through alternative configurations

My .eslintrc file is very simple: { "extends": [ "twilio" ] } However, when I run eslint, I encounter this error message: The config "standard" was referenced from the config file in "/Users/MyAccount/Projects/my-sample-app/node_modules/cipher ...

Obtaining the id in JavaScript from PHP to display information

This is the code I've written: <textarea id="summernote<?php echo $u->id ?>" name="isi" placeholder="Write here" style="width: 100%; height: 100px !important; font-size: 14px; line-height: 18px; border: ...

Retrieving attribute values when using the .on function in jQuery

I currently have 10 links with the following format: <a href="#" data-test="test" class="testclass"></a> as well as a function that looks like this: $(document).on("click", ".testclass", function () { alert($(this).attr('data-t ...

Navigating through a nested array within a JSON object using Vue.js - a guide

I have a JSON data array that includes outer and inner subarrays. My goal is to loop through both levels and create a table. Below you'll find a snippet of the sample array: { class:'I', subDdiv:[ { ...