2014-10-20 5 views
0

이 코드가 있습니다defer.promise 객체가 아닌 데이터를 인쇄

app.factory('loadDependencies', function ($q, $timeout) { 

    return { 
     load: function() { 
      console.log("start 1"); 
      var defer = $q.defer(); 


      $timeout(function() { 
       defer.resolve({ resolve: "got dependencies" }); 
      }, 3000); 

      return defer.promise; 

     } 
    } 
}); 

을 그러나 문제는 제한 시간이 끝날 때까지 기다리지 않는 defer.promise이며, 또한 단지 객체 properteis에서 인쇄 데이터가 아니라면, 다음과 같이 인쇄하십시오.

Object { then: qFactory/defer/deferred.promise.then(), catch: qFactory/defer/deferred.promise.catch(), finally: qFactory/defer/deferred.promise.finally() } 

내가 뭘 잘못했는지 이해하려고합니까?

편집

난 달성하기 위해 노력했다에 대한 몇 가지 추가 정보를 원하시면.

동적으로 컨트롤러 및 CSS 파일을로드해야하는 루트에서 해결 방법이 있습니다. 당신의 timeout이 완료 될 때까지 기다리지 않고 또한 데이터를 반환하지 않습니다 defer.promise 때문이고

.when('/url', { 
     templateUrl: 'someview', 
     controller: 'somecontroller', 
     resolve: { 
      load: function (loadDependencies) { 
       loadDependencies.load(); // here i need to know get the result of what's inside $timeout of 'load' 
      } 
     } 

    }) 
+0

본인의 예에서 볼 수 있듯이 약속을 반환합니다. 당신이 얻고있는 것은 3 초 후에 해결 될 보류중인 약속입니다. – cbass

+0

약속이 해결 될 때까지 반품을 연기 할 수있는 방법이 있습니까? – emc

+0

왜 그랬습니까? 그것의 목적은 무엇인가? – cbass

답변

1

. 그래서 당신이 그것으로 하는가하면 일부 핸들러를 할당하고 /이 완료 될 때까지 기다려 실패 : 약속이 완료 될 때

function success(data) { 
    //this is called after defer.resolve({ resolve: "got dependencies" }); gets executed 
    //here you can access your data 
} 

function error(error) { 
    //this is called if the promise gets rejected 
} 

loadDependencies.load().then(success, error); 

지금, 자동으로 그 핸들러 중 하나를 호출합니다. .finally() 메소드와 같은 콜백을 적용 할 수있는 가능성은 더 많습니다. 을 살펴보십시오.

편집 :

는 그냥 해결에 약속을 반환 :

약속 해결과 각도 후 이제 컨트롤러과 같이 그것을 해결 된 값을 전달합니다 호출됩니다
.when('/url', { 
     templateUrl: 'someview', 
     controller: 'somecontroller', 
     resolve: { 
      load: function (loadDependencies) { 
       return loadDependencies.load(); // here i need to know get the result of what's inside $timeout of 'load' 
      } 
     } 
    }) 

:

app.controller("CtrlName", function($scope, load){ 
    //access your load value here 
}); 
+0

하지만 그 차이점은 무엇입니까? 보류중인 약속이 바로 나옵니다. – cbass

+0

네,하지만 OP가 '약속'을 제대로받지 못했던 것 같아서 제가 설명하고 싶었습니다. 더 자세한 내용을 알지 못하면이 시점에서 구체적인 답을 제시 할 방법이 없습니다. –

+0

나는이 질문을 편집했다. 잘하면 내가 성취하고자하는 것에 대해 조금 더 명확해질 것이다. – emc

관련 문제