角度数据绑定到多个引导面板

时间:2015-04-02 08:05:15

标签: angularjs twitter-bootstrap-3

我需要扩展这个工作示例,以包含多个面板,每个面板都显示自己的数据集。

我目前有两个独立的控制器,并将每个面板绑定到每个控制器,每个控制器都包含各自的数据集。但是,面板的数量需要是动态的,并根据数据馈送的内容进行构建。

显然,我真的不想构建一个单独的控制器。创建单个数据模型然后将过滤后的结果集绑定到每个面板更有意义,例如根据{{​​1}}

进行过滤和绑定
panel_id:

HTML代码:

$scope.items = [
     {
       title: 'Header - 1',
       content: 'Panel 2-Dynamic Group Body - 1',
       panel_id: '1'
     },
     {
       title: ' Header - 2',
       content: 'Panel 2-Dynamic Group Body - 2',
       panel_id: '2'
     },
     {
       title: ' Header - 3',
       content: 'Panel 2-Dynamic Group Body - 3',
       panel_id: '3'
     }
   ];

这是javascript代码

 <!doctype html>
 <html ng-app="ui.bootstrap.demo">
 <head>
   <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
   <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.js"></script>
   <script src="app.js"></script>
   <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
   <link href="style.css" rel="stylesheet"/>
 </head>
 <body>

 <!-- Panel 1 -->

 <div ng-controller="showhideCtrl_1">
   <div class="panel panel-default">
     <!-- Default panel contents -->
     <div class="panel-heading">Panel 1 heading</div>
     <button class="btn btn-default " ng-show="hidevar" ng-click="hidevar=false">Back</button>
     <!-- List group -->
     <ul class="list-group" ng-hide="hidevar">
       <li class="list-group-item" ng-repeat="item in items" ng-click="showdes(item)"><a>{{item.title}}</a></li>
     </ul>
     <div class="panel-body" ng-show="hidevar">
       {{itemdesc.content}}
     </div>
   </div>
 </div>

 <!-- Panel 2 -->

 <div ng-controller="showhideCtrl_2">
   <div class="panel panel-default">
     <!-- Default panel contents -->
     <div class="panel-heading">Panel 2 heading</div>
     <button class="btn btn-default " ng-show="hidevar" ng-click="hidevar=false">Back</button>
     <!-- List group -->
     <ul class="list-group" ng-hide="hidevar">
       <li class="list-group-item" ng-repeat="item in items" ng-click="showdes(item)"><a>{{item.title}}</a></li>
     </ul>
     <div class="panel-body" ng-show="hidevar">
       {{itemdesc.content}}
     </div>
   </div>
 </div>
 </body>
 </html>

这是我到目前为止的一个有效例子:

http://plnkr.co/edit/l65Q3MiofrNn5DyCgCfM?p=preview

1 个答案:

答案 0 :(得分:3)

制作一个指令,该指令将作为一个面板并传递该指令的项目。属于该面板的每个HTML都将转到其模板:

angular.module('ui.bootstrap.demo').directive('showHide', function() {
  return {
    restrict: 'E',
    scope: {
      items : '=',
      index: '='
    },
    template: '<div class="panel panel-default">\
         <!-- Default panel contents -->\
         <div class="panel-heading">Panel {{index}} heading</div><button class="btn btn-default " ng-show="hidevar" ng-click= "hidevar=false">Back</button>\
         <!-- List group -->\
         <ul class="list-group" ng-hide="hidevar" >\
           <li class="list-group-item" ng-repeat="item in items" ng-click="showdes(item)"><a>{{item.title}}</a> </li>\
         </ul>\
         <div class="panel-body" ng-show="hidevar">\
           {{itemdesc.content}}\
         </div>\
       </div>',
    link: function(scope, elm, attr) {
      scope.showdes = function(item){     
          scope.itemdesc=item;
          scope.hidevar=true;
      }
    }
  };
});

然后你可以创建一个具有项目列表列表的控制器:

angular.module('ui.bootstrap.demo').controller('main', function ($scope) {

 $scope.itemsList = [[
    {
      title: 'Header - 1',
      content: 'Panel 1-Dynamic Group Body - 1'
    },
    {
      title: ' Header - 2',
      content: 'Panel 1-Dynamic Group Body - 2'
    },
    {
      title: ' Header - 3',
      content: 'Panel 1-Dynamic Group Body - 3'
    }
  ],[
    {
      title: 'Header - 1',
      content: 'Panel 2-Dynamic Group Body - 1'
    },
    {
      title: ' Header - 2',
      content: 'Panel 2-Dynamic Group Body - 2'
    },
    {
      title: ' Header - 3',
      content: 'Panel 2-Dynamic Group Body - 3'
    }
  ]];


});

在你的HTML中,让ngRepeat为每个可用列表动态创建一个指令:

<div ng-repeat="list in itemsList">
   <show-hide items="list" index="$index + 1"></show-hide>
</div>

请参阅此plunker