2

我在服务中有这个:

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

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

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

4

1 回答 1

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;
    }
   );
 };
于 2013-03-03T22:41:47.440 回答