2015-01-06 4 views
0

데이터가 API 응답에 종속적 인 지시문이 있습니다. API가 완료되기 전에 $compile의 지시문이 실행되기 때문에 지시문 상태가 잘못되었습니다.API가 완료 될 때 초기화 지시문

어떻게 해결할 수 있습니까? API가 완료 될 때까지 요소를 알려주는 지시문, 컨트롤러 또는 AngularJs의 구성이 있습니까?

+0

예제없이 말할 수 없습니다. 지침에서 어떤 유형의 상태가 종속적입니까? –

+0

가능하면 코드 일부를 게시 할 수 있습니까? – Kailas

+0

API가 완료 될 때까지 입력 된 경로의 HTML을로드 할 수 있습니까? – puppeteer701

답변

1

하나의 가능한 해결책은 API가 완료 될 때 ng-if 지시문을 사용하여 dom을 만드는 것입니다.

<your-directive ng-if="isAPIExecuted"></your-directive> 

그리고 당신의 API 성공 처리기에서 ,

Test.get().then(function(){ 
    $scope.isAPIExecuted = true; 
}); 

편집 : 당신은 UI 라우터를 사용하는 경우 API가 실행되면

, 다음 HTML을로드, 당신이 우리가 해결할 수 있습니다

.state{ 
    controller: '', 
    templateUrl: '', 
    resolve: { 
      data: function($q, $timeout){ 
       // Replace this code with your API call 

       var deferred = $q.defer(); 
       $timeout(function(){ 
        return deferred.resolve(); 
       },10000); 

       return deferred.promise; 
      } 
     } 
} 

그래서 약속이 해결되면 HTML과 컨트롤러가로드됩니다.

0

라우터의 API 호출을 해결 예 :

$routeProvider 
.when("/news", { 
    templateUrl: "newsView.html", 
    controller: "newsController", 
    resolve: { 
     message: function(messageService){ 
      return messageService.getMessage(); 
    } 
} 

})

그럼 당신은 해결 컨트롤러 속성을 삽입 할 수 있습니다

app.controller("newsController", function (message) { 
    $scope.message = message; 
}); 

가에서 위의 코드를 얻었다 http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx 자세한 내용을 확인하십시오.

좀 더 구체적인 것을 원하면 코드를 입력하십시오.

관련 문제