2016-08-17 11 views
2

私はエンドユーザーがドロップダウンからクエリを実行し、データの表を表示するアプリケーションを開発中です。テーブル内には、各行にボタンがあります。エンドユーザーがボタンをクリックした場合、後で使用するために、プログラミング内でその行の値を取得する必要があります。ボタンをクリックして行データを取得Angularjsをクリック

これは私の既存のアプリケーションでは機能しないようです。私は角度をつけて新しく、角度を適用せずにコードをコピーすると、これを簡単に実行できるので、これが問題になる可能性があります(下記参照)。

https://jsfiddle.net/auLczhv7/

ここで(このためのRESTエンドポイントはSharePointリストである)私のコードです:

 var spApp = angular.module('spApp', []); 
     spApp.controller('spListCtrl', function ($scope, $http) { 
       $scope.clickButton = function(){ 
        var storeNum = $("#storeNum").val(); 
        $http(
         { 
         method: "GET", 
         url: "https://mypage.com", 
         headers: { "Accept": "application/json;odata=verbose" } 
         } 
        ).success(function(data){ 
         $scope.MyData = data.d.results;   

        }).error(function (data, status, headers, config) { 
       }); 
     } 
       $scope.disputeButton = function(){ 
        var stNum = $(this).parent().siblings(":first-child").text(); 
        alert(stNum); 
     } 
    }); 

そして、私のhtml:

<body> 
    <div id="headerWrap"> 
     <div id="headerText">My App</div>  
    </div> 
    <div ng-app="spApp" id="appWrap"> 
     <div ng-controller="spListCtrl"> 
      <div id="storeSearchWrap"> 
       <span style="font-family:Arial, Helvetica, sans-serif;font-weight:bold">Store Number: </span><select id="storeNum"> 
        <option value="" class="storeNumOpt"></option> 
       </select> 
       <button id="searchBtn" ng-click="clickButton()">Search</button> 
       <button id="printBtn" onclick="printWindow()">Print Report</button> 
      </div> 
      <div id="spinner" style="padding-top:50px;padding-bottom:50px"> 
       <div id="txtWrap"> 
        <p id="waitTxt">Loading...</p>      
      </div> 
     </div> 

      <p id="searchContainer">Filter: <input type="text" ng-model="search"></p> 
      <table width="100%" cellpadding="10" cellspacing="2" class="myTbl-table"> 
       <thead> 
        <tr> 
         <th>Store Number</th> 
         <th>Date</th> 
         <th>Sub</th> 
         <th>Lot</th> 
         <th>Line</th> 
         <th>Sku</th> 
         <th>PO Number</th> 
         <th>Qty Received</th> 
         <th>Dispute Qty</th> 
        </tr>   
       </thead> 
       <tbody> 
        <tr ng-repeat="item in MyData| filter : search"> 
         <td>{{item.StoreNumber}}</td> 
         <td>{{item.Date}}</td> 
         <td>{{item.Sub}}</td> 
         <td>{{item.Lot}}</td> 
         <td>{{item.Line}}</td> 
         <td>{{item.Sku}}</td> 
         <td>{{item.PONumber}}</td> 
         <td>{{item.QtyReceived}}</td> 
         <td><button class="disputeBtn" ng-click="disputeButton()">Dispute</button></td> 

        </tr>   
       </tbody>   
      </table>  
     </div> 
    </div> 
</body> 

私が間違って何をしているのですか?これを角度で達成する簡単な方法はありますか?

答えて

0

クリックした行から値を取得するためにjQueryを使用するのではなく、clickイベントで項目を渡すだけです。大まかに言えば、Angularアプリケーションでは、jQueryを使用してDOMから値を読み取ろうとしている場合、あなたは「何か間違っている」と思っています。ほとんどの場合、あなたがしようとしていることを行う「角度のある方法」があります。それが働いて、

$scope.disputeButton = function(item){ 
    alert(item.StoreNumber); 
} 
+0

素晴らしい:この場合、私はこれまで、あなたのボタンを変更します

<button class="disputeBtn" ng-click="disputeButton(item)">Dispute</button> 

そして、これにあなたのdisputeButton()方法を変更します!私はそれが私がよく知っているので、私はjQueryに戻っている傾向があるので、私はそれを前進しています。ありがとう! – Nate

関連する問題