在视图中未更新$ http调用后,Angularjs范围变量

时间:2013-03-03 16:58:07

标签: scope angularjs digest

我在服务中有这个:

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}}

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

有没有人有任何想法?谢谢。

1 个答案:

答案 0 :(得分:1)

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;
    }
   );
 };