2014-04-06 5 views
3

오늘은 Restangular와 Node로 작업하기 시작했고 각도보기의 사용자 목록에 새 사용자를 추가 할 때 문제가 발생했습니다. view.html오브젝트가 구형 오브젝트를 알지 못합니다.

<input type="text" ng-model="app.user.name" /> 
<input type="button" ng-click="app.addUser(app.user)" /> 

<ul> 
    <li ng-repeat="user in app.users"> 
     <strong>{{ user.name }}</strong>  
     <input type="button" ng-click="app.removeUser(user)" />   
    </li> 
</ul> 

는 위의 한 작은, 아직 성가신 문제를 제외하고도 모두 좋은 작품과

var baseUsers = Restangular.all('users'); 

app.getUsers = function() 
{ 
    baseUsers.getList().then(function(res) 
    { 
     app.users = res; 
    });  
}; 
app.getUsers(); 

app.addUser = function(newUser) 
{    
    baseUsers.post(newUser).then(function(res) 
    { 
     if(res.success == true) 
     { 
      // add new user to scope array 
      app.users.push(res.data); // res.data contains the newly created user 
     }  
    }); 
} 

app.removeUser = function(oldUser) 
{  
    //... 
} 

을 app.js.

새 사용자를 추가하면 사용자 이름이보기의 목록에 추가됩니다. 하지만이 사용자 옆에있는 삭제 버튼을 클릭하면 TypeError: Object #<Object> has no method 'remove' at app.removeUser 오류가 발생합니다. 내 생각 엔 새로 만든 사용자를 범위 배열에 추가 할 때 그것이 어떻게 든 Restangular 객체라는 것을 알지 못한다는 것입니다. 그래서 나는 thik 문제가 거짓말 app.users.push(res.data);

어떻게이 문제를 해결할 수 있습니까?

추신 : 페이지를 새로 고침하면 삭제 기능이 작동합니다. 따라서 push을 통해 수동으로 항목 하나를 추가하는 대신 app.getUsers을 통해 모든 항목을 자동으로 다시 가져옵니다.

+0

과 같을 수 있습니다 ... 그 도움을합니까? – dsmithco

답변

6

정확히 말씀하신대로입니다. 새 사용자가 배열에 밀어 넣은 것은 간단히 "재조직"되지 않았습니다. Restangular에서 restangularizeElement 함수를 살펴보고 새로받은 사용자를 사용자 배열로 보내기 전에 초기화하십시오. 그런

뭔가 :

if(res.success == true) 
{ 
    // add new user to scope array 
    app.users.push(
     Restangular.restangularizeElement('', res.data, 'users') 
    ); 
} 

빈 문자열은 사용자가 부모 자원이 없다는 것을 의미한다.

0

레일 앱에서도 비슷한 문제가있었습니다. 나는 당신의 제거 기능을 보지 않는다, 그래서 나는 추측하고있다. 그러나 기본적으로 Angular는 새로 추가 된 사용자의 ID가 무엇인지 알지 못하므로 사용자를 만들 때 API 응답에서 가져 오는 것이 필요합니다. 제 경우에는 연락이었습니다. 새 연락처

$scope.addContact = function() { 

    // Use the 'then' method to pass the response 'addedContact' back to you 
    Restangular.all('contacts').customPOST($scope.newContact).then(function(addedContact){ 
     // THIS IS WHERE YOU SET THE ID 
     $scope.newContact.id = addedContact.id; 
     // Now it will push the new ID too 
     $scope.contacts.push($scope.newContact); 

    }); 
};  

에 대한

내 범위 그래서 당신의 아래 답변을 참조하십시오이

app.addUser = function(newUser) 
{    
    baseUsers.post(newUser).then(function(res) 
    { 
     if(res.success == true) // BTW you might not need this 1st 'then' function is success 
     { 
      // Get the ID from the res 
      newUser.id = res.data.id // assuming that this will set the ID 

      // add new user to scope array 
      app.users.push(newUser); // res.data contains the newly created user 
     }  
    }); 
} 
관련 문제