How can I send a variable from AngularJS to Java?

18 views
Skip to first unread message

Lohe221

unread,
Feb 5, 2020, 12:42:53 PM2/5/20
to Angular and AngularJS discussion
Hi everyone. I've been trying to send a variable inside of a JS Controller to my Java server using $http, but I cant get it to work: once I try it out, I cant enter the HTML view in which this JS code is used, so the problem is coming from the JS controller. Here's what I am doing and the code (in which I left some doubts), based on trying to adapt these two resources:


angular
.module('myApp')
.controller('MessageController', MessageController);

MessageController.$inject = ['$scope', '$stateParams', '$uibModalInstance', 'MessageTemplate', 'JhiLanguageService'];

onFormatChange.$inject = ['$resource', '$http', 'ServerURL']; // should it be vm.onFormatChange.$inject instead? 

function MessageController ($scope, $stateParams, $uibModalInstance, MessageTemplate, JhiLanguageService) {
    var vm = this;
    vm.isSaving = false;

    vm.text = 'Activated'; // this is the only variable
                           // im trying to send to Java

    function onSaveSuccess (result) {
        // a console.log
    }

    function onSaveError (error) {
        // a console.log
    }

    // this function gets called from the HTML via ng-change
    vm.onFormatChange = function () {
        vm.text = vm.status ? 'Deactivated' : 'Activated';

        // Sending vm.text (as "format") to the server 
        $http.post(ServerURL + 'api/format/', {
            format: vm.text // should it be format = vm.text?
            }. {withCredentials: true,
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'Access-Control-Allow-Origin': true,
                    'X-Requested-With': 'XMLHttpRequest'
                }
        }).then(onSaveSuccess, onSaveError);
    }
}



Reply all
Reply to author
Forward
0 new messages