当元素传递给角度服务时,范围变量不会更新

时间:2015-04-20 15:58:45

标签: javascript angularjs

我有这样的范围变量:

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

//etc..

我有一个事件绑定到一个调用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...
};

我的payload没有更新。无论我在UI中输入什么内容,它们仍然是调用$scope.init()时的默认值,我无法弄清楚原因。

1 个答案:

答案 0 :(得分:1)

想出来。

功能

$scope.checkBackEnd = function ()

应该改写为

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

然后使用

调用单击按钮
ng-click="checkBackEnd(myVarA,myVarB....etc"

由于某种原因,$scope.myVar将在未作为参数传入时调用默认值。

相关问题