在控制器init上调用Angular资源函数仅适用于init

时间:2014-04-23 10:05:10

标签: angularjs controller resources

我有一个应用程序,需要一个像:

这样的网址
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而没有任何乐趣。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你试过这样打电话吗?

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

答案 1 :(得分:0)

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

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