2017-06-05 67 views
1

我想的表下方顯示內容,從一個JSON文件,相對於到項目的用戶表中的角JS - 根據項目更改狀態點擊

例點擊:

JSON文件

{ 
    "patentInfo": [ 
     { 
      "name": "item1", 
      "cost": "$33", 
      "date": "13/06", 
     }, 
     { 
      "name": "item2", 
      "cost": "$29", 
      "date": "02/02", 
     } 
    ] 
} 

視圖 - 表

click on item 1 > DISPLAY price:$33, date:13/06

click on item 2 > DISPLAY price:$29, date:02/02

我這最後一週question問但我不認爲我做它太清楚沒有得到迴應。我使用ui-view來顯示內容,並且它是狀態patents.list.item需要相對的URL,取決於用戶點擊的內容。任何想法如何實現這一目標?

var app = angular.module('myApp', ['ngRoute', 'angularMoment', 'ui.router', "chart.js"]); 

app.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', function($stateProvider, $locationProvider, $urlRouterProvider) { 

    $urlRouterProvider 
        .when('', '/patents/list-patents') 
        .when('/', '/patents/list-patents') 
        .when('/patents', '/patents/list-patents') 
        .when('/transactions', '/transactions/current-transactions') 
        .otherwise('/patents/list-patents'); 

    $stateProvider 
    .state("patents", { 
     url: "/patents", 
     templateUrl: "templates/patents/patent-nav.htm", 
     controller: "patentCtrl" 
    }) 
    .state("patents.list", { 
     url: "/list-patents", 
     templateUrl: "templates/patents/list/list-patents.htm", 
     controller: "patentCtrl" 
    }) 

    .state("patents.list.item", { 
     url: "/patent-item", 
     templateUrl: "templates/patents/list/patent-item.htm", 
     controller: "patentCtrl" 
    }) 

控制器

app.controller('patentCtrl', ['$scope', '$http', 'patentTabFactory', 'loadPatents', '$stateParams', 'patentService', function($scope, $http, patentTabFactory, loadPatents, $stateParams, patentService) { 

    patentService.items.then(function (patents) { 

     $scope.items = patents.data; 
     console.log($scope.patents); 
     $scope.patents = patents.data[patentService.getPatentItem($scope.items, "aid", $stateParams.id)]; 

    }); 






    $scope.patent={id:null,applicationNumber:'',clientRef:'',costRenew:'',renewalDate:'',basketStatus:'', costEnd: '', nextStage:''}; 
    $scope.patents=[]; 
    $scope.submit = $scope.submit; 
    $scope.remove = $scope.remove; 

    $scope.fetchAllPatents = function(){ 
     loadPatents.fetchAllPatents() 
      .then(
      function(d) { 
       $scope.patents = d; 
      }, 
      function(errResponse){ 
       console.error('Error while fetching Users'); 
      } 
     ); 
    } 

    $scope.fetchAllPatents(); 

    $scope.deletePatent = function(id){ 
     loadPatents.deletePatent(id) 
      .then(
      $scope.fetchAllPatents, 
      function(errResponse){ 
       console.error('Error while deleting Patent'); 
      } 
     ); 
    } 

    $scope.submit = function() { 
     if($scope.patent.id===null){ 
      console.log('Saving New User', $scope.patent); 
      loadPatents.createPatent($scope.patent); 
     } 
     console.log('User updated with id ', $scope.patent.id); 

    } 

    $scope.remove = function(id){ 
     console.log('id to be deleted', id); 
     if($scope.patent.id === id) {//clean form if the patent to be deleted is shown there. 
      reset(); 
     } 
     $scope.deletePatent(id); 
    } 

    $scope.tabs = patentTabFactory.tabs; 
    $scope.currentTab = patentTabFactory.currentTab; 
    // $scope.isActiveTab = patentTabFactory.isActiveTab(); 
    $scope.onClickTab = function(currentTab) { 
     patentTabFactory.onClickTab(currentTab); 
     $scope.currentTab = patentTabFactory.currentTab; 
    }; 

    $scope.isActiveTab = function(tabUrl) { 
     return tabUrl == patentTabFactory.currentTab; 
    } 
}]); 

列表專利查看

<div class="row"> 
    <div class="col-md-12"> 
     <table class="table table-bordered table-striped text-md-center"> 
      <thead class="thead-inverse"> 
       <tr> 
        <td ng-click="patentAppOrder()" class="align-middle">Application No. </td> 
        <td class="align-middle">Client Ref</td> 
        <td class="align-middle">Cost to renew</td> 
        <td class="align-middle">Basket</td> 
        <td class="align-middle">Remove</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr ng-repeat="x in patents"> 
        <td><a ui-sref="patents.list.item">{{x.applicationNumber}}</a></td> 
        <td ng-bind="x.clientRef"></td> 
        <td ng-bind="x.costToRenew">$</td> 
        <td ng-bind="x.renewalDueDate"></td> 
        <td><button type="button" class="btn btn-danger" ng-click="remove(x.id)">Remove</button></td> 
       </tr> 
      </tbody> 
     </table> 
     <div ui-view></div>  
    </div> 
</div> 
<div ui-view></div>  

專利項目視圖

<div id="tabs"> 
    <ul> 
     <li ng-repeat="tab in tabs" 
      ng-class="{active:isActiveTab(tab.url)}" 
      ng-click="onClickTab(tab)">{{tab.title}}</li> <!--applies active to the returned tab url --> 
    </ul> 
    <div id="mainView"> 
     <div class="row"> 
      <div ng-include="currentTab"></div> 
     </div> 
    </div> 
</div> 
<script type="text/ng-template" id="patent-info.htm"> 
    <div class="col-md-6 text-xs-center"> 
     <h2>Application Number</h2> 
     <table class="table table-striped"> 
      <tbody> 
       <table> 
        <tr ng-repeat="(x, y) in patents"> 
         <td ng-repeat="x.id in patents"></td> 
         <td>{{y.applicationNumber}}</td> 
        </tr> 
       </table> 
      </tbody> 
     </table> 
    </div> 
    <div class="col-md-6 text-xs-center"> 
     <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
     </p> 
    </div> 
</script> 
<script type="text/ng-template" id="cost-analysis.htm"> 
    <div class="col-md-6 text-xs-center" ng-controller="LineCtrl"> 
     <h2>Cost Analysis</h2> 
     <canvas class="chart chart-line" chart-data="data" chart-labels="labels" 
chart-series="series" chart-click="onClick"></canvas> 
    </div> 
</script> 
<script type="text/ng-template" id="renewal-history.htm"> 
    <div class="col-md-6 text-xs-center"> 
     <h2>Renewal History</h2> 
     <p>In pellentesque faucibus vestibulum. Nulla at nulla justo, eget luctus tortor..</p> 
    </div> 
</script> 
+0

我建議你包含你已經定義了你的路線的三個控制器和模板。 –

+0

相當多的代碼,我可以包括,但我認爲它更多地試圖找出實現這一任務的邏輯。我現在更新 –

+0

您不必包含所有內容。只是與這個問題有關的部分。例如,現在我不知道你的文章中的HTML屬於哪個模板... –

回答

2

你要做的第一件事就是爲路由添加一個動態URL參數,該參數應該顯示關於選定專利的信息並給予另一個控制器。

.state("patents.list.item", { 
    url: "/patent-item/:name", 
    templateUrl: "templates/patents/list/patent-item.htm", 
    controller: "patentDetailsCtrl" 
}) 

然後,當您瀏覽該專利時,您必須在鏈接中包含名稱(假設它是唯一標識符)。

<td><a ui-sref="patents.list.item({ name: x.name })">{{x.applicationNumber}}</a></td> 

之後,在你的patentDetailsCtrl,您可以獲取有關從JSON文件中的專利信息,並顯示它。

替代解決方案

另一種解決方案將處理它在你的patentCtrl而無需額外的(patents.list.item)航線。但我不會推薦這種方法。

如果您使用的是AngularJS v1.5 +,也可以使用組件。要了解更多有關組件的信息,請致電see the official documentation

+0

謝謝你的解釋和例子。袒護我幾分鐘,讓這個工作,我會接受你的答案完成後 –

+0

所有排序。再次感謝你! –

+0

太棒了!樂意效勞。 –