2014-01-20 4 views
0

되지 않습니다 :AngularJS와 자원 객체 a.push 내가 자원 객체를 생성 한 기능

factory('TextResource', 
    function($resource) { 
     return $resource(adminBaseUrl+'/texts/:type', {}, { 
      create: {method: 'POST', params: {type:'create'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}}, 
      update: {method: 'POST', params: {type:'update'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}}, 
      query: {method: 'GET', params: {type: 'list'}}, 
      remove: {method: 'POST', params: {type: 'remove'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}}, 
      getText: {method: 'GET', params: {type: 'get', id:'@id'}} 
     }); 
    } 
) 

을 그리고 내 컨트롤러는 다음과 같습니다

controller('EditText', ['$scope', '$location', '$routeParams', 'TextResource', 'HttpStatusMessage', 
    function($scope, $location, $routeParams, TextResource, HttpStatusMessage) { 
     $scope.alerts = []; 
     $scope.languages = []; 

     TextResource.getText(
      {id: $routeParams.id}, 
      function(data) { 
       $scope.languages = data.result; 
      }, 
      function(error) { 
       var httpError = new HttpStatusMessage(error.status); 
       $scope.alerts.push({type:'error', msg:httpError.msg}); 
      }); 

     $scope.closeAlert = function(index) { 
      $scope.alerts.splice(index, 1); 
     } 

     $scope.submit = function() { 
      TextResource.update(
       $scope.languages, 
       function(data) { 
        if(data.type == 'success') { 
         $location.path('texts'); 
        } else { 
         $scope.alerts.push({type:data.type, msg:data.message}); 
        } 
       }, 
       function(error) { 
        var httpError = new HttpStatusMessage(error.status); 
        $scope.alerts.push({type:'error', msg:httpError.msg}); 
       }); 
     } 

     $scope.cancel = function() { 
      $location.path('texts'); 
     } 
    } 
]) 

응답 내가 TextResource.getText 요청에서입니다 무엇입니까 : 내가 클릭 제출 이제

{"result":[{"id":"3","value":"This is my first text<br>","key":"my_first_text","language_id":"1","name":"English"},{"id":"3","value":"Ceci est mon premier texte","key":"my_first_text","language_id":"3","name":"French"}],"num_rows":2} 

오류를 표시합니다

Error: a.push is not a function 

응답 개체는 2 개의 키 결과를 포함하고 num_rows 결과는 배열입니다. 내가 리소스 객체에서 isArray 매개 변수를 사용하지 않는 이유는 세션 시간 초과, 액세스가 허용되지 않는 등의 오류가 서버에 발생한 경우입니다. 서버가 객체를 반환하면 오류 메시지가 포함됩니다.

+0

코드 검색 'a.push'는 무엇입니까? – DavidLin

+1

a.push가 angularjs 파일로 작성되었습니다 –

+0

귀하의 행동 중 'isArray'가 정의되지 않았 음을 알 수 있습니다. 설정을 시도 했습니까? . isArray - {boolean =} - 참이면이 액션에 대해 반환 된 객체는 배열입니다 (반환 섹션 참조). – DavidLin

답변

1

문제 등 갱신 함수를 수정하여 해결된다 : I 직접 오류가 발생 업데이트의 배열을 게시 하였다

$scope.submit = function() { 
      TextResource.update(
       {'language':$scope.languages}, 
       function(data) { 
        if(data.type == 'success') { 
         $location.path('texts'); 
        } else { 
         $scope.alerts.push({type:data.type, msg:data.message}); 
        } 
       }, 
       function(error) { 
        var httpError = new HttpStatusMessage(error.status); 
        $scope.alerts.push({type:'error', msg:httpError.msg}); 
       }); 
     } 

. 따라서 다른 키로 캡슐화하면 문제가 해결됩니다.