2017-07-11 56 views
1

我有以下问题。我需要设置active类的父母div单击来自该父母内部的子div。为了说明我将提供一个代码。为了便于阅读,省略其中的一部分。添加ng班级上的点击父母的儿童

HTML

<div class="offer__container" ng-repeat="price in settingsPrices"> 
    ... 
    <div class="offer__container__cta hvr-sweep-to-right">Select</div> 
</div> 

CSS

.selected { 
    border: 2px solid #ffbe10; 
} 
.selected-cta { 
    background-color: #ffbe10; 
} 

正如你可以看到我有offer__container,其获取的一些数据,但NG-重复,我需要能够点击从offer__container__cta添加active样式到pa租用容器,并保持跟踪,就好像我点击另一个div通过ng-repeat呈现它应该采取积极的风格,并将其转移到该div。最好我还想在offer__container__cta上设置一定的风格,比如让它变得活跃。

我向所有解决方案开放。

编辑:这是我想要完成的图片。

enter image description here

+0

您忘记添加CSS。你应该从那里开始。 –

+0

你是对的我的坏我添加了CSS。 – ukaric

回答

1

可以使用ng-class根据表达式动态添加类。在对象 创建一个新的属性,并指定其使用的点击ng-init initially.Then布尔值更改为相反的布尔

<div class="offer__container" ng-repeat="price in settingsPrices" ng-class="{'active': price.activePrice}"> 
    ... 
    <div ng-init="price.activePrice = false" class="offer__container__cta hvr-sweep-to-right" ng-click="changeCls(price,$index)">Select</div> 
</div> 

它添加到ng-click功能

var globalIndex = 0; 
$scope.changeCls = function(price, index) { 
    price.activePrice = !price.activePrice; 
    $scope.settingsPrices[globalIndex].activePrice = false; 
    globalIndex = index; 

} 
+0

工程,但它并没有脱离其他divs的风格。 – ukaric

+0

@ kadza检查更新的答案 –

+0

工程就像一个魅力:)如果我可以感谢你的时间我会。 – ukaric

0

一个解决方案:

<div class="offer__container" ng-repeat="price in settingsPrices" ng-class="{active: activePrice === price.amount}"> 
    ... 
    <div class="offer__container__cta hvr-sweep-to-right" ng-click="activePrice = price.amount">Select</div> 
</div> 
+0

在所有'divs'上设置'selected'类,我当时只需要在一个div上激活它。 – ukaric

+0

你确定你测试了我的代码吗?我看到另一个答案,它将会从你上面的评论中看到行为。 –

+0

我做了它的样式概述了所有div。 – ukaric

1

我已经创建假一个小提琴here和以下是代码。您只需为模型创建一个布尔标志并根据您的需要将其设置为活动或非活动状态。而且,每个选择都会清除之前的选择。

这是很好的模型,而不是在模板内变量创建一个单独的标志,因为它保持模板从这样的标志干净。

我使用selected类,但你可以用你的类,你想给到容器div元素替换它。

CSS:

.selected{ 
    color: red; 
} 

HTML:

<div ng-app="myApp"> 
<div ng-controller="MyCtrl as vm"> 
    <div class="offer__container" data-ng-class="{ selected: price.isSelected}" ng-repeat="price in vm.settingsPrices"> 
    {{price.amount}} 
    <button class="offer__container__cta hvr-sweep-to-right" data-ng-click="vm.select(price)">Select</button> 
</div> 
</div> 
</div> 

的JavaScript:

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

//myApp.directive('myDirective', function() {}); 
//myApp.factory('myService', function() {}); 

function MyCtrl($scope) { 
    var vm = this; 

    vm.settingsPrices = [{ 
     amount: 99, 
     isSelected: false 
    }, 
    { 
     amount: 10, 
     isSelected: false 
    } 
    ]; 

    vm.select = function(price){ 
     vm.removeAllSelection(); 
    price.isSelected = true; 
    } 

    vm.removeAllSelection = function(){ 
     angular.forEach(vm.settingsPrices, function(value, key) { 
      value.isSelected = false; 
       }); 
    } 
} 

angular.module('myApp').controller('MyCtrl', MyCtrl); 
+0

不幸的是,我无法访问的数据我提取我只能得到有限的数据。 – ukaric

+0

这很好,您可以在获取数据时添加这个新成员。或者最后的解决方案是使用内联变量! –

+0

@kadza我已经更新了小提琴以清除以前的选择。 –