在控制器init上调用Angular资源函数只能在init上工作



我有一个应用程序,它需要一个类似的URL

http://localhost:8000/admin/patients?uuid=643fa655-b82a-4c02-b445-62d2c966867d

因此,如果存在uuid,则加载患者数据,该数据使用以下代码:

var loadPatientChart = function(uuid){
    PatientsResource.getPatient({
        patientID:uuid
    }, 
    function(patient){
        $scope.patient = patient.data[0];
        console.log($scope.patient);
    });
};
if ("uuid" in $location.search()) {
    var obj = $location.search();
    loadPatientChart(obj.uuid);
}

我遇到的问题是,我需要稍后在同一控制器上再次调用loadPatientChart,使用:

var unbind = $scope.$on('emmittedPatientUuid', function(event, uuid) {
    console.log('emmittedPatientUuid');
    loadPatientChart(uuid);
});
$scope.$on('$destroy', unbind);

这调用了函数,我可以验证是否返回了数据,是否使用batarang更新了$scope,但视图没有更新。没有可见的更改。如果我删除了对init的调用,那么emit函数将按预期连续工作。我也试过在函数末尾调用$apply,但没有任何乐趣。如有任何帮助,我们将不胜感激。

你试过这样称呼它吗?

function(patient){
    $scope.$apply(function(){
      $scope.patient = patient.data[0];
      console.log($scope.patient);
    })
});

原来这是一个愚蠢的错误。我应该使用$rootScope$发射,因为广播在$rootScope 上

var unbind = $rootScope.$on('emmittedPatientUuid', function(event, uuid) {
    loadPatientChart(uuid);
});
$scope.$on('$destroy', unbind);

最新更新