繁体   English   中英

$ http调用后的Angularjs作用域变量未在视图中更新

[英]Angularjs scope variable after $http call isn't updated in the view

我在服务中有这个:

UpdateQuestionsSmsStatus: function (smsProfileId, active) {
    //TODO: ajax get data
    var promise = this.UpdateQuestionsSmsStatusInternal(smsProfileId, active).then(function (response) {
        // The return value gets picked up by the then in the controller.
        return response;
    });
    // Return the promise to the controller
    return promise;
},

UpdateQuestionsSmsStatusInternal: function (smsProfileId, active) {

    return $http({ method: 'GET',
        url: '../QuestionManagement/UpdateQuestionsSmsStatus?smsProfileId=' + smsProfileId + '&active=' + active
    }).
      success(function (data, status, headers, config) {
          // this callback will be called asynchronously
          // when the response is available
          response = data;
      }).
      error(function (data, status, headers, config) {
          // called asynchronously if an error occurs
          // or server returns response with an error status.
          if (window.console && console.log) {
              console.log("Could not obtain questions received. Error:" + data + "Status:" + status + "Headers:" + headers + "Config:" + config);
          }
      });
},

这在我的控制器中:

$scope.updateQuestionsSmsStatus = function () {
    questionSelectionService.UpdateQuestionsSmsStatus($scope.smsProfile.Id, !$scope.smsProfile.Active).then(function (output) {
        $scope.smsProfile.Active = (output.data.result == "success") ? output.data.active : !output.data.active;
    });

};

在视图中称为onclick。 在视图中:

{{smsProfile.Active}}

视图中的值永远不会更新。 调试器没有显示任何错误,没有错误出现。

有人有什么想法吗? 谢谢。

您在UpdateQuestionsSmsStatusInternal函数中的成功回调需要返回一个值:

return $http({ method: 'GET',
        url: '../QuestionManagement/UpdateQuestionsSmsStatus?smsProfileId=' + smsProfileId + '&active=' + active
    }).
      success(function (data, status, headers, config) {
          return data;
      })
      ...

为了适应这种情况,控制器的updateQuestionsSmsStatus函数应调整回调:

$scope.updateQuestionsSmsStatus = function () {
  questionSelectionService.UpdateQuestionsSmsStatus($scope.smsProfile.Id, !$scope.smsProfile.Active).then(function (data) {
      $scope.smsProfile.Active = (data.result == "success") ? data.active : !data.active;
    }
   );
 };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM