Kyle
Kyle

Reputation: 5537

Scope variables not updating when elements are passed onto angular service

I have scope variables like this:

//initialize variables
$scope.init = function () {
    $scope.myVarA = 10;
    $scope.myVarB = 5;
    $scope.myVarC = new Date();
    $scope.myVarD = new Date();
};

//etc..

I have an event bound to a button that calls for checkBackEnd():

//using angular promises
$scope.checkBackEnd = function () {

    //prepare the payload, here `myVar--` don't get updated when the user enter in new values
    var payload = "/?myVarA=" + $scope.myVarA+ "&myVarB=" + $scope.myVarB
        + "&myVarC=" + dateFormat($scope.myVarC, "mm/dd/yyyy HH:MM:ss") + "&myVarD=" + dateFormat($scope.myVarD, "mm/dd/yyyy HH:MM:ss");

    //send it to the api service
    apiService.calculate(payload).then... etc...
};

My payload isn't updating. They remain the default value of when $scope.init() was called, no matter what I input into the UI, and I can't figure out why.

Upvotes: 0

Views: 45

Answers (1)

Kyle
Kyle

Reputation: 5537

Figured it out.

The function

$scope.checkBackEnd = function ()

should be rewritten to

$scope.checkBackEnd = function (myVarA,myVarB,myVarC,myVarD) {

Then the click button be called with

ng-click="checkBackEnd(myVarA,myVarB....etc"

For some reason $scope.myVar will call the defaulted values when not passed in as arguments.

Upvotes: 1

Related Questions