2014-11-25 4 views
1

각도 js 팩터럼에 List<KeyValuePair<string, byte>>을 반환하는 웹 API가 있습니다. 그런 다음 선택을 사용하여 선택 ->자바 스크립트의 각도 js 배열을 반복합니다.

<select ng-model="survey.surveyType" class="form-control" id="surveyType"> 
        <option ng-repeat="surveyType in surveyTypes" value="{{surveyType.value}}">{{surveyType.key}}</option> 
       </select> 

이 작동합니다. 각도 루프가 객체를 올바르게 표시합니다. 웹 api 방법으로 이야기 할 때 나는 이것을 이렇게 구성했습니다 ->

app.factory('Survey', ['$resource', function ($resource) { 
return $resource('http://localhost:52133/api/surveyapi/:id', null, 
    { 
     'update': { method: 'PUT' }, 
     'getSurveyTypesAsDictionary': { method: 'GET', isArray: true, url: 'http://localhost:52133/api/surveyapi/GetSurveyTypesAsDictionary' } 
    }); 
}]); 

// fetching the values in my controller 
var surveyTypes = Survey.getSurveyTypesAsDictionary(); 

이제 내 문제는 이것입니다. 결과를 반복 할 수 없습니다. >

enter image description here

을하지만 내 각 배열 내부의 값을 가져하려고하면 아무것도 없다 - 콘솔에서 객체는 다음과 같습니다. 예를 들어 surveyTypes.length은 0입니다.

질문. JavaScript를 사용하여 배열을 어떻게 소비 할 수 있습니까? 감사합니다.

편집 : 웹 API에서 Dictionary<string, byte> 만 반환하려했으나 전혀 작동하지 않았습니다. 세르주에

편집, 코멘트 :

var promise = Survey.getSurveyTypesAsDictionary(); 
promise.$promise.then(function (response) { 
    console.log(response.length); 
}); 

내가 개체에서 $promise를 얻을 수 있습니다 귀하의 제안 작업을 확인합니다. 이것이 맞습니까? 아니면 구문을 작성하는 방식으로 작동하도록 공장을 구성해야합니까?

+4

는'getSurveyTypesAsDictionary'가 _asynchronous_의 요청을하고 _promise_을 반환 돌아왔다. 'Survey.getSurveyTypesAsDictionary(). then (function (response) {// 응답을 사용할 수 있습니다.});'. –

+0

가능한 [Ajax 호출 응답을 반환하는 방법?] (http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) –

+0

감사 Sergiu , 편집에서 후속 질문을했습니다 :) – Andreas

답변

4

먼저 반환 된 resource object에는 하위 수준 $http 서비스와 상호 작용하지 않고도 높은 수준의 동작을 제공하는 동작 방법이 있습니다. 당신은 또한 객체에 $promise 속성을 통해 원시 $http 약속을 액세스 할 수 있습니다

예를

Post.query(function(data) { 
    $scope.posts = data; 
}); 

를 들어

하거나,

var req = Post.query().$promise; 
req.then(function(data) { 
    $scope.posts = data; 
});