2017-04-14 116 views
0

我正在使用表格,並且表格中的按鈕彈出一個模式。我想將該行中的Id值傳遞給模態控制器,以便我可以使用它將其傳遞給其餘的api調用,然後在模態表中加載valus。將ng單擊值傳遞給一個控制器並在另一個控制器中使用它

App.js

var app = angular.module("UiApp", ["ServiceApp"]); 

app.service('sharedProperties', function() { 
    var idValue = 'test string value'; 

    return { 
     getId: function() { 
      return idValue; 
     }, 
     setId: function (value) { 
      idValue = value; 
     } 
    } 
}); 

app.controller("PortFolioController", function ($scope, GetPortfolios,sharedProperties) { 
    $scope.Portfolios = GetPortfolios.query({ pmid: 2 }); 
    console.log($scope.Portfolios); 
    $scope.addOrder = function (id) { 
     sharedProperties.setId(id) 
    }; 
}); 

app.controller("OrderController", function ($scope, GetOrders,sharedProperties) { 

    $scope.item = sharedProperties.getId(); 
    $scope.Orders = GetOrders.query({ id: item}); 
}); 

Service.js

var app = angular.module("ServiceApp", ["ngResource"]); 

    app.factory('GetPortfolios', function ($resource) { 
     return $resource("http://localhost:61347/api/PortfolioManager/GetPortfolios/"); 
    }); 

    app.factory('GetOrders', function ($resource) { 
     return $resource("http://localhost:61347/api/PortfolioManager/GetPortfolioOrders/"); 
    }); 

HTML

<div > 
             <table class="table table-striped table-hover table-bordered" id="editable-sample" ng-controller="PortFolioController"> 
              <thead> 
               <tr> 
                <th>Portfolio ID</th> 
                <th>Portfolio Name</th> 
                <th>Portfolio Type</th> 
                <th>Portfolio Description</th> 
                <th>Show Stocks</th> 
               </tr> 
              </thead> 
              <tbody> 
               <tr class="" ng-repeat="portfolio in Portfolios"> 

                <td>{{portfolio.portfolioId}}</td> 
                <td>{{portfolio.portfolioName}}</td> 
                <td>{{portfolio.type}}</td> 
                <td>{{portfolio.description}}</td> 
                <td> <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal" ng-click="addOrder(portfolio.portfolioId)" >Show <i class="fa fa-info-circle"></i></button></td> 
               </tr> 
              </tbody> 
             </table> 
            </div> 

           </div> 
           <!--Modal start--> 

           <div class="modal fade" id="myModal" role="dialog"> 
            <div class="modal-dialog"> 

             <!-- Modal content--> 
             <div class="modal-content"> 
              <div class="modal-header"> 
               <button type="button" class="close" data-dismiss="modal">&times;</button> 
               <h4 class="modal-title">My Portfolio</h4> 
              </div> 
              <div class="modal-body"> 
               <h3>Stock List</h3> 
               <div class="space15"></div> 
               <table class="table table-striped table-hover table-bordered" id="editable-sample" ng-controller="OrderController"> 
                <thead> 
                 <tr> 
                  <th>Symbol</th> 
                  <th>Stock Name</th> 
                  <th>Quantity</th> 
                  <th>Market Price</th> 
                 </tr> 
                </thead> 
                <tbody> 
                 <tr class="" ng-repeat="order in Orders"> 
                  <td>{{order.symbol}}</td> 
                  <td>{{order.executedQuantity}}</td> 
                  <td>{{order.price}}</td> 
                  <td>{{order.createTStamp}}</td> 
                 </tr> 

                </tbody> 
               </table> 
              </div> 
              <div class="modal-footer"> 
               <button type="button" class="btn btn-primary" data-dismiss="modal">Close <i class="fa fa-times"></i></button> 
              </div> 
+1

有什麼問題? – manish

+0

@manish它顯示項目值沒有定義。 –

+0

你使用[UIBootstrap](https://angular-ui.github.io/bootstrap/)嗎?如果是這樣,那麼你正在尋找[解析]選項(只需在該頁面上查找它們 - 它們不會輕鬆直接鏈接)。 –

回答

0

或沒有任何service是使用$rootScope的simpleset方式和$broadcast一個event從一個Controller並聽取它在其他Controller

因此,無需編寫額外的服務sharedProperties

事情是這樣的: -

app.controller("PortFolioController", function ($scope, GetPortfolios,$rootScope) { 
    //Other Codes 
    $scope.addOrder = function (id) { 
    $rootScope.$broadcast('ID-Clicked', { Id: id}); 
    }; 
} 

app.controller("OrderController", function ($scope, GetOrders,$rootScope) { 
    //Other Codes 
    //Listen for the event 
    $rootScope.$on('ID-Clicked', function(event, data){ 
     $scope.item = data.Id;// data.Id has the Clicked Id 
    }); 
} 
+0

我是否需要在OrderController中傳遞GetPortfolio? –

+0

不,我很抱歉,我正在複製我的初始代碼,並忘記擺脫那項服務,編輯我的答案 – manish

+0

嘿,我試過了。它不工作 –

0

這種方法可以用兩種方式來完成。

第一種方法是從父根目錄實例中廣播一些事件並捕獲$ scope元素上的內容。這種格式不被認爲是一種很好的編碼方式。其次,要在控制器之間進行數據通信使用一項服務。是的,你目前在右邊,清理&被廣泛接受的路徑。

請記住,服務singletons.so一旦instansiated它是提供給所有controllers.But這裏的挑戰是,只要你按鈕元素點擊你的服務在service.Now設置您idValue如何第二控制器知道。答案是註冊一個$ watch服務來觀察服務中的idValue是否改變如下。

app.controller("OrderController", function ($scope, GetOrders, sharedProperties) { 
       $scope.$watch(function() { 
        return sharedProperties.getId() 
       }, function (newValue, oldValue) { 
        if (newValue != oldValue) { 
         $scope.item = newValue; 
         $scope.Orders = GetOrders.query({ id: item }); 
        } 
       }); 

      }); 
相關問題