2013-03-03 72 views
2

我有這樣的服務:Angularjs範圍變量在視圖中不更新

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

視圖中的值永遠不會更新。調試器不顯示任何錯誤,不會出現錯誤。

有沒有人有任何想法?謝謝。

+0

是否調試器中看到的數據一路回到控制器?例如,是否在最後一個承諾中填充了output.data? – 2013-03-03 18:14:39

+0

@RoyTruelove是的 – Para 2013-03-03 18:24:05

+0

是使用onclick還是ng-click?如果它是前者,那麼你應該將onclick回調包裝在$ scope。$ apply中。如果它是後者,那麼其他事情就會發生,因爲ng-click包裝在$申請中。 – 2013-03-03 18:39:15

回答

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