AngularJS相当于jQuery切换显示/隐藏一个部分

时间:2013-03-06 12:18:40

标签: javascript angularjs

首先,是的,我尝试使用谷歌搜索,但仍然很难找到有关AngularJS的信息。

我想根据表单中按下的按钮来执行打开部分的简单任务。我希望只有一个部分可以在任何时间打开,也许是默认部分(尚未决定)。 如果您单击的按钮将被归类为“btn-primary”用于引导程序,那也很好。所以这是html

<form>
    <input type="button" id="show-section1" value="Section 1" />
    <input type="button" id="show-section2" value="Section 2" />
    <input type="button" id="show-section3" value="Section 3" />
</form>
<section id="section1">blah</section>
<section id="section2">blah2</section>
<section id="section3">blah3</section>

在jQuery中我会做这样的事情(简化而不是解释的最佳解决方案):

$('section').hide();
$('#show-section1').click(function() {
    $('section').hide();
    $('#section1').show();
});
etc

之前我曾经做过这个,但是我记不起来了,我记得它的代码行很少。

3 个答案:

答案 0 :(得分:7)

如果您一次只需要一个,可以使用:http://jsfiddle.net/jHhMv/3/

JS:

'use strict';

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

function Ctrl($scope){
    var section = 1;

    $scope.section = function (id) {
        section = id;   
    };

    $scope.is = function (id) {
        return section == id;
    };
}

HTML:

<div ng-controller="Ctrl">
<form>
    <input type="button" id="show-section1" value="Section 1" ng-click="section(1)" ng-class="{'btn-primary': is(1)}" />
    <input type="button" id="show-section2" value="Section 2" ng-click="section(2)" ng-class="{'btn-primary': is(2)}" />
    <input type="button" id="show-section3" value="Section 3" ng-click="section(3)" ng-class="{'btn-primary': is(3)}" />
</form>
<section id="section1" ng-show="is(1)">blah</section>
<section id="section2" ng-show="is(2)">blah2</section>
<section id="section3" ng-show="is(3)">blah3</section>
</div>

答案 1 :(得分:6)

看看http://jsfiddle.net/mahbub/jHhMv/

<div ng-controller="Ctrl">
<form>
    <input type="button" id="show-section1" value="Section 1" ng-click="section1=!section1" />
    <input type="button" id="show-section2" value="Section 2" ng-click="section2=!section2" />
    <input type="button" id="show-section3" value="Section 3" ng-click="section3=!section3" />
</form>
<section id="section1" ng-show="section1">blah</section>
<section id="section2" ng-show="section2">blah2</section>
<section id="section3" ng-show="section3">blah3</section>
</div>


'use strict';

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

function Ctrl($scope){

}

答案 2 :(得分:3)

有很多方法。其中之一(使用AngularUI):

HTML:

<div ng-controller="AppController">

    <button ng-click="setActive('section1')" ng-class="{'btn btn-primary': active.section1}">Section 1</button>
    <button ng-click="setActive('section2')" ng-class="{'btn btn-primary': active.section2}">Section 2</button>
    <button ng-click="setActive('section3')" ng-class="{'btn btn-primary': active.section3}">Section 3</button>

    <section id="section1" ui-toggle="active.section1">blah</section>
    <section id="section2" ui-toggle="active.section2">blah2</section>
    <section id="section3" ui-toggle="active.section3">blah3</section>

</div>

CSS:

.ui-show {
  opacity: 1;
  transition: all 0.5s ease;
}
.ui-hide {
  opacity: 0;
  transition: all 0.5s ease;
}

JS:

app.controller('AppController',
  function($scope) {
    $scope.active = {section1: true};
    $scope.setActive = function(section){
      $scope.active = {};
      $scope.active[section] = true;
    };
  }
);

Plunker