2013-03-06 4 views
6

Prima di, sì, ho provato a cercare su Google, ma è ancora difficile trovare informazioni su AngularJS.AngularJS equivalente a jQuery attiva/nasconde una sezione

Desidero eseguire una semplice operazione di apertura di sezioni in base a quale pulsante viene premuto nel modulo. Voglio che una sola sezione sia aperta in qualsiasi momento e forse una di default (non ho deciso). Sarebbe anche bello se il pulsante su cui si fa clic sarà classificato "btn-primary" per bootstrap. Quindi questo è il 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> 

In jQuery vorrei fare qualcosa di simile (semplificato e non la soluzione migliore solo per spiegare):

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

Ho fatto questo una volta prima ma posso' Ricordo come, ricordo che c'erano pochissime righe di codice.

risposta

7

Se avete solo bisogno uno a un tempo, puoi usare questo: 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> 
+0

Dove definisci 'miaApp'? : S – OZZIE

+0

è solo un nome di modulo. Puoi mettere "OZZIE" lì se vuoi – Ven

+0

+1, mi piace questa soluzione perché funziona anche con HTML dinamico, come nel mio scenario. Gli ID nel mio caso sono generati al volo e non possono essere codificati nell'html. Grazie! – Vaibhav

6

Prendere uno sguardo 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){ 

} 
+0

E 'solo una dimostrazione su per la mostra nascondi. Per il tuo caso, l'angolare non è assolutamente necessario. – Mahbub

+1

OP: "Voglio che una sola sezione sia aperta in qualsiasi momento". La tua soluzione consente a più dispositivi di essere aperti contemporaneamente. –

3

molti modi. Uno di loro (utilizzando 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

+0

Tutto ciò che ottengo è: "Errore: Argument 'AppController' non è una funzione, non definito" – OZZIE

+0

Cosa dovrebbe '' var app = angular.module ('plunker', ['ui']); '' be? Un'altra risposta qui usa "myApp" ma dove la imposto? Ho : S – OZZIE

+0

Ok ho creato un controller vuoto '' funzione AppController ($ scope) {} '' supponevo .. – OZZIE

Problemi correlati