2014-12-10 161 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

您可以发布完整的控制器代码。 – 2014-12-10 23:10:00

回答

2

如果我没有弄错,你没有创建一个持久存储属性的editBox的新实例,所以guest.editBox.signature仅在editBox函数运行时存在,它不会持久。

你可以做的是建立在范围内一个新的变量编辑框之外,像

this.signature = {}; 

然后内编辑框,你可以签名对象分配到新创建的this.signature。与

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

在你editForm你可以叫NG提交如果有必要,在submitEdit结束时,你可以重置回this.signature一个空的对象。

p.s是否有原因不使用$ scope?