2014-10-11 4 views
0

그래서이 멋진 프로젝트가 있습니다. 블루투스 LE, javascript로 네이티브 코드를 호출하여 알리고 ....AngularJS에서 뷰가 업데이트되지 않음

이제 우리는이 혼란에 갇혀 있습니다.

처음에는 데이터 바인딩이 매우 좋았지 만 작업을 마친 후에는 점점 악화되었습니다.

controllers.controller('DashboardCtrl', function($scope) { 
    $scope.as = Demo.results[GATT.Dashboard.as]; 
    $scope.ac = Demo.results[GATT.Dashboard.ac]; 
    $scope.av = Demo.results[GATT.Dashboard.av]; 
    $scope.ps = Demo.results[GATT.Dashboard.ps]; 
    $scope.cs = Demo.results[GATT.Dashboard.cs]; 
    $scope.ts = Demo.results[GATT.Dashboard.ts]; 
    $scope.dd = Demo.results[GATT.Dashboard.dd]; 
    $scope.rc = Demo.results[GATT.Dashboard.rc]; 
    $scope.tps = Demo.results[GATT.Dashboard.tps]; 
    $scope.ls = Demo.results[GATT.Dashboard.ls]; 
    $scope.bm = Demo.results[GATT.Dashboard.bm];  
}); 

Demo.results 목적은 우리의 블루투스 장치에서 밀려 값을 원하는 분야 그래서 여기

는 코드입니다.

이제보기에 "간단히"표시해야합니다.

$ apply는 $ digestion cycle에 오류가 있기 때문에 위의 예는 왼쪽 코드입니다.

$ watch가 작동하지만보기에서 변경된 값을 표시 할 방법이없는 것처럼 보입니다.

변수가 변경되었음을 알릴 수는 있지만 값이 실제로 뷰에 표시되도록 할 수는 없습니다.

이 시스템에서 우리가하는 것보다 더 많은 것을 알고있는 사람이 있습니까? 왜냐하면 이것은 angularj에 대한 가장 가치있는 것들 중 하나이기 때문입니다. 그것은 단지 부러졌습니다. 아니면 우리가 망 쳤어.

답변

0

$scope.safeApply() 메서드를 사용해보십시오. 그리고이 메소드에서 뷰 업데이트 코드를 래핑하십시오. 그것은 확인도 업데이트됩니다

controllers.controller('DashboardCtrl', function($scope) { 
    $scope.safeApply($scope.viewUpdate()); // Call this code from the intended function, like an API call back or something where you wish to get your view uodated. 

    $scope.safeApply = function(fn) { 
     var phase = this.$root.$$phase; 
     if(phase == '$apply' || phase == '$digest') { 
     if(fn && (typeof(fn) === 'function')) { 
     fn(); 
     } 
     } else { 
     this.$apply(fn); 
     } 
    }; 

    $scope.viewUpdate = function() { 

    $scope.as = Demo.results[GATT.Dashboard.as]; 
    $scope.ac = Demo.results[GATT.Dashboard.ac]; 
    $scope.av = Demo.results[GATT.Dashboard.av]; 
    $scope.ps = Demo.results[GATT.Dashboard.ps]; 
    $scope.cs = Demo.results[GATT.Dashboard.cs]; 
    $scope.ts = Demo.results[GATT.Dashboard.ts]; 
    $scope.dd = Demo.results[GATT.Dashboard.dd]; 
    $scope.rc = Demo.results[GATT.Dashboard.rc]; 
    $scope.tps = Demo.results[GATT.Dashboard.tps]; 
    $scope.ls = Demo.results[GATT.Dashboard.ls]; 
    $scope.bm = Demo.results[GATT.Dashboard.bm]; 

    } 
}); 

참조 : https://coderwall.com/p/ngisma/safe-apply-in-angular-js

관련 문제