2014-12-10 3 views
0

컨트롤러의 다른 함수간에 변수를 전달하려고합니다.각도 전달 변수

HTML :

<table class="flat-table"> 
      <tr> 
       <th>User</th> 
       <th>Comment</th> 
       <th>Date</th> 
       <th>Controls</th> 
      </tr> 
      <tr ng-repeat="doc in guest.docs"> 
       <td>{{doc.value.user}}</td> 
       <td>{{doc.value.comment}}</td> 
       <td>{{doc.value.date}}</td> 
       <td> 
        <img class="controls" src="styles/delete.png" ng-click="guest.delete(doc.id)" title="Delete"> 
        <img class="controls" src="styles/edit.png" ng-click="guest.visible = true; guest.editBox(doc.id)" title="Edit"> 
       </td> 
      </tr> 
     </table> 
     <div id="signCon"> 
      <form name="addForm" ng-submit="guest.add()"> 
       <textarea type="text" ng-model="guest.signature.comment" id="comment" placeholder="Enter a comment?!" required></textarea> 
       <br/> 
       <input type="submit" value="Sign!"> 
      </form> 
     </div> 
    </div> 
    <div id="editCon" ng-show="guest.visible === true"> 
     <h1>Edit</h1> 
     <p>Here you can alter your comment.</p> 
     <form name="editForm" ng-submit="guest.submitEdit(guest.editBox.signature)"> 
      <textarea type="text" ng-model="guest.submitEdit.comment" required></textarea> 
      <br/> 
      <input type="submit" value="Edit!"> 
     </form> 
    </div> 

ANGULAR :

this.editBox = function(id) { 
      var that = this; 
      this.id = id; 
      this.signature = {}; 
      if(self.visible) { 
       $http({ 
        url: 'http://ip:5984/guestbook/' + this.id, 
        method: 'GET', 
        withCredentials: true, 
        headers: { 
         'Authorization': auth_hash(UserService.get().username, UserService.get().password) 
        } 
       }).success(function(data, status, headers, config) { 
        that.signature = data; 
        console.log(that.signature); 
       }).error(function(data, status, headers, config) { 
        console.log("error!") 
       }); 
      }; 
     }; 
     this.submitEdit = function(signature){ 
      var self = this; 
      this.comment = ''; 
      this.signature = signature; 
      console.log(this.signature); 
     }; 

아이디어는 그 편집 이미지에 사용자가 클릭 새 창이 나타납니다 때 여기에 내가이 일을 사용하고있는 코드는 그들은 새로운 코멘트를 입력하고 다시 제출할 수 있습니다. 창이 올바르게 표시되고 개체를 올바르게 당길 수 있습니다. submitEdit 함수를 호출하려고 할 때입니다. 서명 변수를 통과하지 않는 것 같습니다. 이 일을 제대로하고 있습니까?

+0

전체 컨트롤러 코드를 게시 할 수 있습니까? –

답변

2

내가 잘못 본 것이 아니라면 속성을 지속적으로 저장하는 editBox의 새 인스턴스를 만들지 않으므로 guest.editBox.signature는 editBox 함수가 실행되는 동안에 만 존재하며 유지되지 않습니다.

은 당신이 할 수있는 것은, 편집 상자의 외부

this.signature = {}; 

그런 다음 편집 상자 내에는 새로 만든 this.signature에 서명 객체를 할당 할 수있는 뭔가를 범위에 새 변수를 만드는 것입니다. 필요한 경우 editForm에서

호출 할 수 submitEdit의 끝에서,

ng-submit="guest.submitEdit(guest.signature)" 

와 NG 제출, 당신은 빈 오브젝트로 this.signature를 재설정 할 수 있습니다.

p.s가 $ scope을 사용하지 않는 이유가 무엇입니까?