2014-04-17 4 views
6

$ scope의 일부 페이지에서 일부 텍스트를 업데이트하려고합니다. 하지만 계속이 오류가 발생합니다.

Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply][1] 
at Error (native) 
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:6:450 
at m (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:101:443) 
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:301) 
at h.$scope.changeLang (http://treenovum.es/xlsmedical/js/medical-app.js:80:16) 
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:169:382 
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:390 
at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:40) 
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:318) 
at HTMLAnchorElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:372) 

분명히 뭔가 잘못하고 있습니다. :) 어떻게 해결할 수있는 아이디어? 범위 내의 새 변수로 페이지를 업데이트하고 싶습니다.

이것은 내가 업데이트를 위해 사용하고 코드는 다음과 같습니다

medicalApp.controller('MainCtrl', function($scope, $cookies, getTranslation) { 
    getTranslation.get(function(data){ 
     $scope.translation = data; 
    }); 

    $scope.changeLang = function (lang) { 
     console.log(lang); 
     $cookies.lang = lang; 
     $scope.$apply(function(){ 
      getTranslation.get(function(data){ 
       $scope.translation = data; 
       console.log(JSON.stringify($scope.translation)); 
      }); 
     }); 
    }; 
}); 

html로 :

<body ng-controller="MainCtrl"> 
    ... 
      <div class="header-lang col-xs-4"> 
       <p> 
        <a href="#" ng-click="changeLang('de')">DE</a> | 
        <a href="#" ng-click="changeLang('fr')">FR</a></p> 
      <div>{{ translation.text }}</div> <---- Example variable I want updated. 
    ... 

나는 또한 내가로드 각 페이지에 대해 별도의 컨트롤러와 ngRoute을 사용하고, 그 경우 그걸로 할 일이 뭐야?

답변

8

changeLang 내부에 $scope.$apply(...)을 사용하고 있으므로 '이미 다이제스트 있음'오류가 발생합니다. ng-click이 (가) 이미 처리했기 때문에 $ scope ($) (...) 블록에 getTranslation으로 전화 할 필요가 없습니다. 그걸 꺼내면 그냥 작동해야합니다. 또한, 콘솔 용으로보다 나은 오류를 볼 수 있도록 개발되지 않은 버전의 dev 용으로 실행하는 것이 좋습니다.

+0

내가 잘 모르겠어요 솔루션이 작동하는지 여부는 문제가 아니라는 것을 알았습니다. getTranslation은 페이지가로드 될 때 호출되는 팩토리입니다. 그러나 두 번째 호출시이를 무시합니다. 어떻게 해결할 수 있는지 알고 있습니까? –

+0

getTranslation 코드를 봐야하지만 서비스의 'singleton' 수명을 처리하는 것처럼 들립니다. Dependency Injection 컨테이너에서 싱글 톤 인스턴스를 각도로 dole하는 것을 기억하십시오. – SonOfNun

16

모델을 변경 한 후에 템플릿이 변경되지 않고 $scope.$apply을 사용해야하는 경우이 대신 $scope.$applyAsync을 사용할 수 있습니다.

https://github.com/angular-ui/ui-codemirror/issues/73

+0

이것은 내 문제를 해결합니다. 고마워요 !! –

+0

멋진! 이것으로 인해 많은 어려움을 겪었습니다. –

관련 문제