Получить связанный контент таблетки в соответствии с нажатием img в Angular - PullRequest
0 голосов
/ 07 ноября 2018

Учитывая следующий сценарий:

  • Угловой 1.7.2
  • Bootstrap 3

У меня возникают некоторые проблемы после того, как пользователь нажимает на img:

ЧТО Я ПОЛУЧАЮ

При нажатии на любое изображение появляется модальное окно с нужным содержимым

нажмите IMG 1: все отлично работает

нажмите IMG 2/3/4: таблетки не действуют

Нажав на вкладки MODG IMG 2/3/4, а затем нажав на IMG 1, Таблетки показывают содержание последнего клика от 2/3/4, относящегося к IMG 1.

например. если я нажимаю IMG 4 - TAB 3, а затем нажимаю IMG 1, я получаю содержимое IMG 1 TAB 3, даже если я нахожусь в TAB 1

Чего я хочу получить

При нажатии на любой IMG появляется всплывающее модальное окно с нужным контентом и специфичной для него pills

ВОПРОС МОЖЕТ ПРИБЫТЬ ИЗ

Я предполагаю, что описанная проблема возникает из id из pills, который остается тем же на каждом модале.

Я не хочу / не могу использовать: angular-ui.github.io/bootstrap/#!#modal решить вопрос

var app = angular.module('app', ['ngSanitize']);

app.controller('MainController', ['$scope',
function($scope){
	
	
	$scope.mainTitle = 'MEET THE TEAM',
	$scope.headLine = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
	$scope.teamSection = 'EXECUTIVE TEAM',
	
		
	$scope.modals = [
		{
			bio: "TAB 1",
			experience: "TAB 2",
			motto: "TAB 3"
		}
	];	
		
	
	$scope.teams = [
		{
		name : 'name1',
		role : 'role1',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here1', 
		experience : 'experience text here1',
		author : 'some more text1',
		mobile: 'READ MORE'
	
		},
		
		{
		name : 'name2',
		role : 'role2',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here1', 
		experience : 'experience text here2',
		author : 'some more text2',
		mobile: 'READ MORE'
		},
		{
		name : 'name3',
		role : 'role3',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here1', 
		experience : 'experience text here3',
		author : 'some more text3',
		mobile: 'READ MORE'
		},
		{
		name : 'name4',
		role : 'role4',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here1', 
		experience : 'experience text here4',
		author : 'some more text4',
		mobile: 'READ MORE'
		}
	];
	
}]);
body,
html {
  margin: 0;
  padding: 0;
  font-family: Montserrat;
  height: 100%;
}

.p0 {
  padding: 0;
}

.container {
  margin-top: 50px;
}

h1 {
  text-align: center;
  font-size: 35px;
  padding-bottom: 20px;
}

h2 {
  text-align: center;
  font-size: 15px;
  line-height: 150%;
  padding-bottom: 50px;
}

h3 {
  text-align: center;
  font-size: 14px;
  font-weight: 600;
}

.flex-parent {
  display: flex;
  width: 100%;
  height: 50px;
  align-items: center;
  padding: 50px 0;
}

.flex-child-edge {
  flex-grow: 2;
  height: 1px;
  background-color: #e3e3e3;
  border: 0.5 #e3e3e3 solid;
}

.flex-child-text {
  flex-basis: auto;
  flex-grow: 0;
  margin: 0px 5px 0px 5px;
  text-align: center;
  padding: 20px;
  font-size: 13px;
  line-height: 150%;
  color: #777777;
  letter-spacing: 3px;
}

.flex-parent-second {
  height: 150px;
}

.person {
  text-align: center;
  margin-bottom: 50px;
}

.person img {
  cursor: pointer;
}

.name {
  font-size: 15px;
  margin-top: 10px;
  padding-bottom: 8px;
  letter-spacing: 3px;
  line-height: 150%;
}

.role {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 2px;
  line-height: 150%;
}

.line {
  width: 50px;
  height: 1px;
  background-color: #e3e3e3;
}

.modal-header {
  border-bottom: 0;
}

.roundProfile {
  margin: 0 auto;
  text-align: center !important;
}

.roundProfile img {
  max-width: 150px;
  margin-bottom: 15px;
}

hr {
  max-width: 30%;
}

.modal-open .modal {
  background-color: rgba(255, 255, 255, 0.8);
}

.modal-content {
  background-image: url(../images/e.png);
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: center;
  box-shadow: 0 0 10px #e3e3e3;
  border: none;
}

.nav-pills>li.active>a,
.nav-pills>li.active>a:focus,
.nav-pills>li.active>a:hover {
  background-color: #b04890;
  font-weight: 500;
  color: #ffffff !important;
}

.modal-body {
  padding: 0 50px 50px;
}

.nav-pills {
  text-align: center;
  margin: 30px 0;
}

.nav-pills>li {
  text-align: center;
  float: none;
  display: inline;
  font-size: 13px;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.nav>li>a {
  display: inline;
  padding: 5px 15px;
}

.tab-content p {
  font-size: 13px;
  line-height: 200%;
  text-align: center;
  padding-left: 15px;
  font-weight: 400;
}

.author {
  font-style: italic;
  display: block;
  text-align: center;
  font-size: 11px;
  margin-top: 20px;
}

.flex-parent {
  padding: 0 0 50px 0;
}

.fade {
  opacity: 1 !important;
  transition: opacity .25s ease-in-out;
  -moz-transition: opacity .25s ease-in-out;
  -webkit-transition: opacity .25s ease-in-out;
}

.fade:hover {
  opacity: 0.5 !important;
}

@media (max-width:767px) {
  .red-more-mobile {
    font-size: 10px;
    position: relative;
    bottom: 20px;
    background-color: rgba(255, 255, 255, 0.80);
    padding: 5px 0;
    letter-spacing: 2px;
    margin-bottom: -20px;
  }
  .container {
    padding-left: 0;
    padding-right: 0;
  }
  .modal-body {
    padding: 0px 20px 20px 20px;
  }
  .nav>li>a {
    display: inline;
    padding: 2px 5px;
  }
  .nav-pills>li {
    letter-spacing: 1px;
  }
  .name {
    font-size: 10px;
  }
  .role {
    font-size: 9px;
  }
  h2 {
    padding-bottom: 0;
    padding-left: 15px;
    padding-right: 15px;
  }
  .flex-parent {
    padding: 0;
  }
  .flex-parent-second {
    position: relative;
    top: 0px;
    height: 50px;
    margin-bottom: 50px;
  }
}
<!doctype html>
<html>

<head>
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <meta charset="UTF-8">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <!-- Include the AngularJS library -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.7.5/angular-sanitize.js"></script>

  <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  <!--<script src="js/respond.js"></script>-->
</head>

<body ng-app="app">
  <div class="container" ng-controller="MainController">
    <h1>{{ mainTitle }}</h1>
    <h2>{{ headLine }}</h2>



    <div class="flex-parent">
      <div class="flex-child-edge"></div>
      <div class="flex-child-text">
        <h3>{{ teamSection }}</h3>
      </div>
      <div class="flex-child-edge"></div>
    </div>

    <div class="main" ng-repeat="team in teams">

      <!-- PERSON 1 -->
      <div class="col-xs-6 col-md-3 col-sm-4 person">

        <img ng-src="{{ team.mainImage }}" class="img-responsive center-block fade" alt="{{team.alt}}" data-toggle="modal" data-target="#person{{$index}}" />
        <div class="red-more-mobile visible-xs" data-toggle="modal" data-target="#person{{$index}}">{{team.mobile}}</div>
        <div class="name">{{ team.name }}</div>
        <div class="role">{{ team.role }}</div>
      </div>

      <!-- Modal -->

      <div class="modal " id="person{{$index}}" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="roundProfile row">
              <div class="col-xs-12">
                <img ng-src="{{ team.roundImage }}" class="img-responsive img-circle center-block" alt="" />
              </div>
              <div class="name">{{ team.name }}</div>
              <div class="role">{{ team.role }}</div>
              <hr>
            </div>
            <div class="modal-body" ng-repeat="modal in modals">
              <ul class="nav nav-pills">
                <li class="active"><a data-toggle="pill" href="#home">{{modal.bio}}</a></li>
                <li><a data-toggle="pill" href="#menu1">{{modal.experience}}</a></li>
                <li><a data-toggle="pill" href="#menu2">{{modal.motto}}</a></li>
              </ul>
              <div class="tab-content">
                <div id="home" class="tab-pane  in active">
                  <p ng-bind-html="team.bio"></p>
                </div>
                <div id="menu1" class="tab-pane ">
                  <p>{{ team.experience }}</p>
                </div>
                <div id="menu2" class="tab-pane ">
                  <p>{{ team.motto }}<span class="author">{{ team.author }}</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>



  <!-- Modules -->
  <script src="js/app.js"></script>

  <!-- Controllers -->
  <script src="js/controllers/MainController.js"></script>
</body>

</html>

Ответы [ 2 ]

0 голосов
/ 09 ноября 2018

ЧТО Я СДЕЛАЛ, ЧТОБЫ РЕШИТЬ ЭТО:

  • Удалено ng-repeat="modal in modals" из modal-body
  • Имя табуляции жестко закодировано
  • Используется ...{{$index}} как указано выше modal и предложение пользователя Anees

var app = angular.module('app', ['ngSanitize']);

app.controller('MainController', ['$scope',
function($scope){
	
	
	$scope.mainTitle = 'MEET THE TEAM',
	$scope.headLine = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
	$scope.teamSection = 'EXECUTIVE TEAM',
	
		
	$scope.modals = [
		{
			bio: "TAB 1",
			experience: "TAB 2",
			motto: "TAB 3"
		}
	];	
		
	
	$scope.teams = [
		{
		name : 'name1',
		role : 'role1',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here1', 
		experience : 'experience text here1',
		author : 'some more text1',
		mobile: 'READ MORE'
	
		},
		
		{
		name : 'name2',
		role : 'role2',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here2', 
		experience : 'experience text here2',
		author : 'some more text2',
		mobile: 'READ MORE'
		},
		{
		name : 'name3',
		role : 'role3',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here3', 
		experience : 'experience text here3',
		author : 'some more text3',
		mobile: 'READ MORE'
		},
		{
		name : 'name4',
		role : 'role4',
		mainImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		roundImage : 'https://www.m5hosting.com/wp-content/uploads/no-profile-img.gif',
		bio : 'bio text here4', 
		experience : 'experience text here4',
		author : 'some more text4',
		mobile: 'READ MORE'
		}
	];
	
}]);
body,
html {
  margin: 0;
  padding: 0;
  font-family: Montserrat;
  height: 100%;
}

.p0 {
  padding: 0;
}

.container {
  margin-top: 50px;
}

h1 {
  text-align: center;
  font-size: 35px;
  padding-bottom: 20px;
}

h2 {
  text-align: center;
  font-size: 15px;
  line-height: 150%;
  padding-bottom: 50px;
}

h3 {
  text-align: center;
  font-size: 14px;
  font-weight: 600;
}

.flex-parent {
  display: flex;
  width: 100%;
  height: 50px;
  align-items: center;
  padding: 50px 0;
}

.flex-child-edge {
  flex-grow: 2;
  height: 1px;
  background-color: #e3e3e3;
  border: 0.5 #e3e3e3 solid;
}

.flex-child-text {
  flex-basis: auto;
  flex-grow: 0;
  margin: 0px 5px 0px 5px;
  text-align: center;
  padding: 20px;
  font-size: 13px;
  line-height: 150%;
  color: #777777;
  letter-spacing: 3px;
}

.flex-parent-second {
  height: 150px;
}

.person {
  text-align: center;
  margin-bottom: 50px;
}

.person img {
  cursor: pointer;
}

.name {
  font-size: 15px;
  margin-top: 10px;
  padding-bottom: 8px;
  letter-spacing: 3px;
  line-height: 150%;
}

.role {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 2px;
  line-height: 150%;
}

.line {
  width: 50px;
  height: 1px;
  background-color: #e3e3e3;
}

.modal-header {
  border-bottom: 0;
}

.roundProfile {
  margin: 0 auto;
  text-align: center !important;
}

.roundProfile img {
  max-width: 150px;
  margin-bottom: 15px;
}

hr {
  max-width: 30%;
}

.modal-open .modal {
  background-color: rgba(255, 255, 255, 0.8);
}

.modal-content {
  background-image: url(../images/e.png);
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: center;
  box-shadow: 0 0 10px #e3e3e3;
  border: none;
}

.nav-pills>li.active>a,
.nav-pills>li.active>a:focus,
.nav-pills>li.active>a:hover {
  background-color: #b04890;
  font-weight: 500;
  color: #ffffff !important;
}

.modal-body {
  padding: 0 50px 50px;
}

.nav-pills {
  text-align: center;
  margin: 30px 0;
}

.nav-pills>li {
  text-align: center;
  float: none;
  display: inline;
  font-size: 13px;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.nav>li>a {
  display: inline;
  padding: 5px 15px;
}

.tab-content p {
  font-size: 13px;
  line-height: 200%;
  text-align: center;
  padding-left: 15px;
  font-weight: 400;
}

.author {
  font-style: italic;
  display: block;
  text-align: center;
  font-size: 11px;
  margin-top: 20px;
}

.flex-parent {
  padding: 0 0 50px 0;
}

.fade {
  opacity: 1 !important;
  transition: opacity .25s ease-in-out;
  -moz-transition: opacity .25s ease-in-out;
  -webkit-transition: opacity .25s ease-in-out;
}

.fade:hover {
  opacity: 0.5 !important;
}

@media (max-width:767px) {
  .red-more-mobile {
    font-size: 10px;
    position: relative;
    bottom: 20px;
    background-color: rgba(255, 255, 255, 0.80);
    padding: 5px 0;
    letter-spacing: 2px;
    margin-bottom: -20px;
  }
  .container {
    padding-left: 0;
    padding-right: 0;
  }
  .modal-body {
    padding: 0px 20px 20px 20px;
  }
  .nav>li>a {
    display: inline;
    padding: 2px 5px;
  }
  .nav-pills>li {
    letter-spacing: 1px;
  }
  .name {
    font-size: 10px;
  }
  .role {
    font-size: 9px;
  }
  h2 {
    padding-bottom: 0;
    padding-left: 15px;
    padding-right: 15px;
  }
  .flex-parent {
    padding: 0;
  }
  .flex-parent-second {
    position: relative;
    top: 0px;
    height: 50px;
    margin-bottom: 50px;
  }
}
<!doctype html>
<html>

<head>
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <meta charset="UTF-8">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <!-- Include the AngularJS library -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.7.5/angular-sanitize.js"></script>

  <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  <!--<script src="js/respond.js"></script>-->
</head>

<body ng-app="app">
  <div class="container" ng-controller="MainController">
    <h1>{{ mainTitle }}</h1>
    <h2>{{ headLine }}</h2>



    <div class="flex-parent">
      <div class="flex-child-edge"></div>
      <div class="flex-child-text">
        <h3>{{ teamSection }}</h3>
      </div>
      <div class="flex-child-edge"></div>
    </div>

    <div class="main" ng-repeat="team in teams">

      <!-- PERSON 1 -->
      <div class="col-xs-6 col-md-3 col-sm-4 person">

        <img ng-src="{{ team.mainImage }}" class="img-responsive center-block fade" alt="{{team.alt}}" data-toggle="modal" data-target="#person{{$index}}" />
        <div class="red-more-mobile visible-xs" data-toggle="modal" data-target="#person{{$index}}">{{team.mobile}}</div>
        <div class="name">{{ team.name }}</div>
        <div class="role">{{ team.role }}</div>
      </div>

      <!-- Modal -->

      <div class="modal " id="person{{$index}}" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="roundProfile row">
              <div class="col-xs-12">
                <img ng-src="{{ team.roundImage }}" class="img-responsive img-circle center-block" alt="" />
              </div>
              <div class="name">{{ team.name }}</div>
              <div class="role">{{ team.role }}</div>
              <hr>
            </div>
            <div class="modal-body">
              <ul class="nav nav-pills">
                <li class="active"><a data-toggle="pill" href="#home{{$index}}">TAB 1</a></li>
                <li><a data-toggle="pill" href="#menu1{{$index}}">TAB 2</a></li>
                <li><a data-toggle="pill" href="#menu2{{$index}}">TAB 3</a></li>
              </ul>
              <div class="tab-content">
                <div id="home{{$index}}" class="tab-pane in active">
                  <p ng-bind-html="team.bio"></p>
                </div>
                <div id="menu1{{$index}}" class="tab-pane ">
                  <p>{{ team.experience }}</p>
                </div>
                <div id="menu2{{$index}}" class="tab-pane ">
                  <p>{{ team.motto }}<span class="author">{{ team.author }}</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>



  <!-- Modules -->
  <script src="js/app.js"></script>

  <!-- Controllers -->
  <script src="js/controllers/MainController.js"></script>
</body>

</html>
0 голосов
/ 07 ноября 2018

от вашего вопроса, я полагаю, модалы открываются правильно, но таблетки ведут себя безразлично. Причина этого в том, что у вас одинаковые идентификаторы для вкладок во всех модальных моделях.

<div class="tab-content">
  <div id="home" class="tab-pane  in active">
    <p ng-bind-html="team.bio"></p>
  </div>
   <div id="menu1" class="tab-pane ">
     <p>{{ team.experience }}</p>
   </div>
    <div id="menu2" class="tab-pane ">
  <p>{{ team.motto }}<span class="author">{{ team.author }}</span></p>
</div>
</div>

Изменение ids на что-то вроде menu1_{{$index}} и соответствующих ссылок может решить вашу проблему.
Но в идеале я думаю, что вы должны переосмыслить архитектуру, чтобы это был некий чистый и поддерживаемый код. Я хотел бы предложить что-то вроде: оставить один модал на DOM и динамически загружать содержимое, которое вам нужно, по нажатию любого изображения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...