Modal显示角度ng-repeat数组中的第一个元素

时间:2017-04-10 07:19:50

标签: angularjs angularjs-directive angularjs-ng-repeat bootstrap-modal

此Html代码有助于在卡块中重复公司资料。

 <div class="container-fluid" id="gallery" ng-controller="ComController" style="padding-top: 2em;">
      <h1 class="display-4 text-center" style="margin-bottom: 1em;">Campus Recruiters</h1>
      <div class="row">
        <div class="col-4" ng-repeat="company in companies">
          <div class="card" style="margin-bottom: 2em;">

            <img class="card-img-top img-fluid" ng-src="{{company.com_logo}}" alt="Card image cap">
            <div class="card-block">

              <h5 class="card-title">{{company.com_name}}</h5>
              <p class="card-text">{{company.com_abt}}</p>
              <button class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg" ng-click="setSelectedItem(company)">About {{company.com_name}}</button>
             <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" >
                    <div class="modal-dialog modal-lg" ng-model="selectedItem == company"> //Button to actiavte modal
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title" id="exampleModalLabel">{{company.com_name}}</h5>
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span></button>
                        </div>                  
                      </div>
                    </div>
                  </div>


            </div>
          </div>

        </div>
      </div>
    </div>

这是我正在使用的ComController。 现在我无法弄清楚为什么只有第一个元素才显示,当我点击按钮进行模态时。

app.controller('ComController', ['$scope', function($scope){


    $scope.setSelectedItem = function(item) {
        $scope.selectedItem = item;
    };


    $scope.companies = [

        {

            com_logo: 'recruit_icons/tcs_logo.png',
            com_name: 'Tata Consultancy Services',
            com_abt: 'I am this company',
        },
        {
            com_logo: 'recruit_icons/hcl_logo.png',
            com_name: 'Hindustan Computers Limited',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/hp_logo.png',
            com_name: 'Hewlett Packard',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/ion_logo.png',
            com_name: 'Ion Trading',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/niit_logo.png',
            com_name: 'NIIT Technologies',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/techm_logo.png',
            com_name: 'Tech Mahindra',
            com_abt: 'I am this company',
        },
        {
            com_logo: 'recruit_icons/tcs_logo.png',
            com_name: 'Tata Consultancy Services',
            com_abt: 'I am this company',
        },
        {
            com_logo: 'recruit_icons/hcl_logo.png',
            com_name: 'Hindustan Computers Limited',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/hp_logo.png',
            com_name: 'Hewlett Packard',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/ion_logo.png',
            com_name: 'Ion Trading',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/niit_logo.png',
            com_name: 'NIIT Technologies',
            com_abt: 'I am this company',
        },{
            com_logo: 'recruit_icons/techm_logo.png',
            com_name: 'Tech Mahindra',
            com_abt: 'I am this company',
        }];


}]);

2 个答案:

答案 0 :(得分:0)

试试这个。

<div ng-controller="ComController">
<div ng-repeat="item in companies">
 <img class="card-img-top img-fluid" ng-src="{{item.com_logo}}" alt="Card 
image cap">
    <div class="card-block" >
      <h5 class="card-title">{{item.com_name}}</h5>
      <p class="card-text">{{item.com_abt}}</p>
      <button class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg" ng-click="setSelectedItem(company)">About {{item.com_name}}</button>
     <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" >
            <div class="modal-dialog modal-lg" ng-model="selectedItem == company"> //Button to actiavte modal
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabel">{{item.com_name}}</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span></button>
                </div>                  
              </div>
            </div>
          </div>

       </div>
    </div>
  </div>

答案 1 :(得分:0)

乍一看,我在你的HTML中发现了一些小错误。

<div class="modal-dialog modal-lg" ng-model="selectedItem == company"> 

语句本身就是因为div上的ng-model给出了非赋值异常。并且ng-model没有声明只保留模态对象的条件。

你可以试试这个。它可能会对你有帮助。

<div ng-controller="ComController">
  <div ng-repeat="item in companies">
    <div class="card-block" >
        <h5 class="card-title">{{item.com_name}}</h5>
        <p class="card-text">{{item.com_abt}}</p>
        <button class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg" ng-click="setSelectedItem(item)">About {{item.com_name}}</button>
        <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" >
            <div class="modal-dialog modal-lg" ng-if="selectedItem.com_name == item.com_name"> //Button to actiavte modal
                <div class="modal-content" style="color: lightblue">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">{{item.com_name}}</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    </div>                  
                </div>
            </div>
        </div>
    </div>
</div>

你的角度代码保持不变。

感谢。