简体   繁体   English

用ng-click切换3个不同的div

[英]Toggle 3 different divs with ng-click

I have three anchors. 我有三个锚。 Each one needs to show a unique, full-width div below it similar to the ipad example on this site https://sendtoinc.com/ 每个人都需要在其下方显示一个唯一的全角div,类似于该网站上的ipad示例https://sendtoinc.com/

Eg I click the first anchor and it opens the first container and the second closes the first and opens the second. 例如,我单击第一个锚点,它将打开第一个容器,第二个则关闭第一个容器,然后打开第二个容器。

Can I use ng-click to toggle an active state to perform this function? 我可以使用ng-click切换活动状态来执行此功能吗? Do i need to do it within the controller? 我需要在控制器内执行此操作吗?

Below is a row with the anchors. 下面是带有锚的行。 The content to be displayed is in a separate container. 要显示的内容在单独的容器中。 Any thoughts or help would be appreciated. 任何想法或帮助将不胜感激。

<div class="row">
        <div class="col-xs-12 col-md-4">
            <a href="" class="box-anchor active">
                <div class="box box-drop ">
                    <h3>Data Explorer</h3>
                    <p>It’s a brave new data world.  Explore and navigate new realms of possibility such as post density, sentiment analysis, and even sort by hue and saturation.</p>
                </div>
            </a>
        </div>
        <div class="col-xs-12 col-md-4">
            <a href="" class="box-anchor">
                <div class="box box-drop">
                    <h3>Analytics</h3>
                    <p>Exciting twists on old standards, Socialight's analytic platform allows you to analyze any social account in the world. Track your competitors and make informed, comparative decisions for your next campaign.</p>
                </div>
            </a>
        </div>
        <div class="col-xs-12 col-md-4">
            <a href="" class="box-anchor">
                <div class="box box-drop">
                    <h3>Export & Embed</h3>
                    <p>Enliven billboards, websites, or TV screens with tailored feeds of tagged content from your Data Explorer  you can Improve your nextTransfer all of this useful information to outward facing products. </p>
                </div>
            </a>
        </div>
    </div>
</div>

Attached is a screenshot of what it looks like. 附件是其外观的屏幕截图。 The top row contains the anchors which change the content below. 第一行包含锚点,这些锚点会更改下面的内容。

在此处输入图片说明

In your markup use either an ng-switch or an ng-if and in your navigation use ng-clicks 在标记中使用ng-switch或ng-if,在导航中使用ng-clicks

<div class="row">
    <div class="col-xs-12 col-md-4" ng-click="page=1">
        <a href="" class="box-anchor active">
            <div class="box box-drop ">
                <h3>Data Explorer</h3>
                <p>It’s a brave new data world.  Explore and navigate new realms of possibility such as post density, sentiment analysis, and even sort by hue and saturation.</p>
            </div>
        </a>
    </div>
    <div class="col-xs-12 col-md-4" ng-click="page=2">
        <a href="" class="box-anchor">
            <div class="box box-drop">
                <h3>Analytics</h3>
                <p>Exciting twists on old standards, Socialight's analytic platform allows you to analyze any social account in the world. Track your competitors and make informed, comparative decisions for your next campaign.</p>
            </div>
        </a>
    </div>
    <div class="col-xs-12 col-md-4" ng-click="page=3">
        <a href="" class="box-anchor">
            <div class="box box-drop">
                <h3>Export & Embed</h3>
                <p>Enliven billboards, websites, or TV screens with tailored feeds of tagged content from your Data Explorer  you can Improve your nextTransfer all of this useful information to outward facing products. </p>
            </div>
        </a>
    </div>
</div>

Use the ng-switch in your content markup. 在内容标记中使用ng-switch。 Note: ng-switch-default is the page that will show when user hasn't selected a page. 注意:ng-switch-default是用户未选择页面时将显示的页面。 Also note if user selects one, it will show the default page because theres no ng-switch that is mapped to "1" in this example. 还要注意,如果用户选择一个,它将显示默认页面,因为在此示例中没有ng-switch映射到“ 1”。

<div ng-switch="page">
    <div ng-switch-default>
        Content page one
    </div>
    <div ng-switch-when="2">
        Content page two
    </div>
    <div ng-switch-when="3">
        Content page three
    </div>
</div>

You could use ng-class to toggle an 'active' class, which would show/hide/transition your content divs. 您可以使用ng-class切换“活动”类,该类将显示/隐藏/转换您的内容div。

<div class="row">
        <div class="col-xs-12 col-md-4">
            <a href="" class="box-anchor" ng-click="myActive = 1">
                <div class="box box-drop ">
                    <h3>Data Explorer</h3>
                    <p>It’s a brave new data world.  Explore and navigate new realms of possibility such as post density, sentiment analysis, and even sort by hue and saturation.</p>
                </div>
            </a>
        </div>
        <div class="col-xs-12 col-md-4">
            <a href="" class="box-anchor" ng-click="myActive = 2">
                <div class="box box-drop">
                    <h3>Analytics</h3>
                    <p>Exciting twists on old standards, Socialight's analytic platform allows you to analyze any social account in the world. Track your competitors and make informed, comparative decisions for your next campaign.</p>
                </div>
            </a>
        </div>
        <div class="col-xs-12 col-md-4">
            <a href="" class="box-anchor" ng-click="myActive = 3">
                <div class="box box-drop">
                    <h3>Export & Embed</h3>
                    <p>Enliven billboards, websites, or TV screens with tailored feeds of tagged content from your Data Explorer  you can Improve your nextTransfer all of this useful information to outward facing products. </p>
                </div>
            </a>
        </div>
    </div>
</div>


<div ng-class="{active: myActive == 1}">Content Area 1</div>
<div ng-class="{active: myActive == 2}">Content Area 2</div>
<div ng-class="{active: myActive == 3}">Content Area 3</div>

With the above, clicking the divs with the ng-click directive will change your scope's myActive variable to the corresponding value. 通过以上操作,使用ng-click指令单击div会将范围的myActive变量更改为相应的值。 That will then give the 'active' class to the section below it. 然后,将在其下一节提供“活动”类。 You could then show/hide content based on the 'active' class, and add some CSS transitions to make it look snazzy. 然后,您可以基于“活动”类显示/隐藏内容,并添加一些CSS过渡以使其显得时髦。

Look at the implementation of angular bootstrap accordion. 查看角度引导手风琴的实现。

It can do a similar thing where it closes all other accordion groups when you expand one. 当您展开一个手风琴组时,它会关闭所有其他手风琴组,因此它可以做类似的事情。

The accordion code is available on github https://github.com/angular-ui/bootstrap/tree/master/src/accordion 可在github https://github.com/angular-ui/bootstrap/tree/master/src/accordion上找到手风琴代码

In particular you are probably interested in the closeOthers function. 特别是您可能对closeOthers函数感兴趣。 Notice you'll have to logically group your divs somehow for this to work. 请注意,您必须以某种方式对div进行逻辑分组才能正常工作。

// Ensure that all the groups in this accordion are closed, unless close-others explicitly says not to 
this.closeOthers = function(openGroup) { 
    var closeOthers = angular.isDefined($attrs.closeOthers) ? $scope.$eval($attrs.closeOthers) : accordionConfig.closeOthers; 
    if ( closeOthers ) { 
        angular.forEach(this.groups, function (group) { 
        if ( group !== openGroup ) { 
            group.isOpen = false; 
        } 
   }); 
 } 
}; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM