2016-09-15 11 views
0

ボタンを追加しようとしていますが、これをクリックするとパラメータを受け取り、それを私のサーバに送る関数を呼び出します。これまでのところ、それは次のようになります。ボタンをクリックしたときに古いパラメータをとる角度。

<table class="table table-hover"> 
    <thead> 
    <tr> 
     <th>Id</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr data-ng-repeat="interview in $ctrl.pendingInterviews"> 
     <td>{{ interview.id }}</td> 
     <td><a href="/#!/pending-interviews-list?interviewId={{interview.id}}"><input type="submit" name="Submit" id="submit" ng-click="$ctrl.addParticipant();"></a></td> 
    </tr> 
    </tbody> 
</table> 

私は私の角度成分を持っているもの:

var participant={ 
        username:"mama", 
        interviewId:$routeParams.interviewId 
       }; 
       console.log(participant); 
       console.log(JSON.stringify(participant)); 

       this.addParticipant = function saveParticipant() { 
        console.log("in partikip") 
        Interview.addParticipant(JSON.stringify(participant)) 
         .then(
          function (errResponse) { 
           console.error('Error while fetching pending interviews'); 
          } 
         ); 
       } 

そして、私は私の角度サービスを持っている:

function addParticipant(participant) { 
       console.log("Im here too"); 
       console.log(participant + "asdasdsda"); 
       var deferred = $q.defer(); 
       $http.post('http://localhost:8080/interviewsupdateparticipant', participant) 
        .then(
         function (response) { 
          deferred.resolve(response.data); 
         }, 
         function (errResponse) { 
          console.error('Error while adding participant'); 
          console.error(''+ participant.username + participant.interviewId) 
          deferred.reject(errResponse); 
         } 
        ); 
       return deferred.promise; 
      } 

問題は、その最初のものです私のページに行くと、コントローラの参加者はユーザー名がmamaに設定され、interviewIdは未定義に設定されます。送信ボタンをクリックすると、IDとハードコードされたユーザー名を送信するのではなく、未定義とハードコードされたユーザー名が送信されます。どうして?なぜ自動的にインタビューIDを取得しないのですか?

[送信]をクリックすると、何らかの理由でIDが未定義のままになり、再度クリックするとIDが変更されます。どのような問題が起こる可能性がありますか?この問題を解決することができ

答えて

2

多くの事:あなたのURLの可変部分を持っているとき

  • ではなくただのhrefのngHrefを使用してみてください
  • それを取得するのではなく、あなたのaddParticipant()メソッドにパラメータとしてinterview.idを渡して試してみてください$routeParams変数
  • を経由してあなたはどのような形態の外input type="submit"を持っている、と<a>リンクの内側に、変更してみてくださいその
+0

どうやら、私はちょうどテストしたし、常に最後の値を保持し、代わりに私が望むものを得るために追加するためにクリックするもの、私はSUbmitを2回押す必要があります! :/理由は分かりません – Mocktheduck

+0

リンク内に入力があるため、リダイレクトが行われる前に入力にバインドされたメソッドが呼び出されることがあります。ボタンを押したままリンクを削除し、パラメータとして 'ng-click =" $ ctrl.addParticipant(interview.id); "と入力し、コントローラ内の正しい$ルートに戻るようにしてください。 –

+0

あなたは正しい!私はそれをして、それは今働く!どうもありがとうございました!何故ならそれがJSONの予期しないトークンDを位置0に投げる理由は何ですか? – Mocktheduck

関連する問題