I am trying to figure out a way to do an expand and collapse using angular js. I haven\'t been able to find an elegant way to do this without manipulating dom objects in the con
See http://angular-ui.github.io/bootstrap/#/collapse
function CollapseDemoCtrl($scope) {
$scope.isCollapsed = false;
}
<div ng-controller="CollapseDemoCtrl">
<button class="btn" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button>
<hr>
<div collapse="isCollapsed">
<div class="well well-large">Some content</div>
</div>
</div>
The problem comes in by me not knowing how to send a unique identifier with an ng-click to only expand/collapse the right content.
You can pass $event
with ng-click (ng-dblclick, and ng- mouse events), then you can determine which element caused the event:
<a ng-click="doSomething($event)">do something</a>
Controller:
$scope.doSomething = function(ev) {
var element = ev.srcElement ? ev.srcElement : ev.target;
console.log(element, angular.element(element))
...
}
See also http://angular-ui.github.com/bootstrap/#/accordion
I just wrote a simple zippy/collapsable using Angular using ng-show, ng-click and ng-init. Its implemented to one level but can be expanded to multiple levels easily.
Assign a boolean variable to ng-show and toggle it on click of header.
Check it out here
You can solve this fully in the html:
<div>
<input ng-model=collapse type=checkbox>Title
<div ng-show=collapse>
Only shown when checkbox is clicked
</div>
</div>
This also works well with ng-repeat since it will create a local scope for each member.
<table>
<tbody ng-repeat='m in members'>
<tr>
<td><input type=checkbox ng-model=collapse></td>
<td>{{m.title}}</td>
</tr>
<tr ng-show=collapse>
<td> </td>
<td>{{ m.content }}</td>
</tr>
</tbody>
</table>
Be aware that even though a repeat has its own scope, initially it will inherit the value from collapse from super scopes. This allows you to set the initial value in one place but it can be surprising.
You can of course restyle the checkbox. See http://jsfiddle.net/azD5m/5/
Updated fiddle: http://jsfiddle.net/azD5m/374/ Original fiddle used closing </input>
tags to add the HTML text label instead of using <label>
tags.
Here a simple and easy solution on Angular JS using ng-repeat that might help.
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
$scope.arr= [
{name:"Head1",desc:"Head1Desc"},
{name:"Head2",desc:"Head2Desc"},
{name:"Head3",desc:"Head3Desc"},
{name:"Head4",desc:"Head4Desc"}
];
$scope.collapseIt = function(id){
$scope.collapseId = ($scope.collapseId==id)?-1:id;
}
});
/* Put your css in here */
li {
list-style:none;
padding:5px;
color:red;
}
div{
padding:10px;
background:#ffffd;
}
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<meta charset="utf-8" />
<title>AngularJS Simple Collapse</title>
<script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
</head>
<body ng-controller="MainCtrl">
<ul>
<li ng-repeat='ret in arr track by $index'>
<div ng-click="collapseIt($index)">{{ret.name}}</div>
<div ng-if="collapseId==$index">
{{ret.desc}}
</div>
</li>
</ul>
</body>
</html>
This should fulfill your requirements. Here is a working code.
Plunkr Link http://plnkr.co/edit/n5DZxluYHi8FI3OmzFq2?p=preview
Github: https://github.com/deepakkoirala/SimpleAngularCollapse
In html
button ng-click="myMethod()">Videos</button>
In angular
$scope.myMethod = function () {
$(".collapse").collapse('hide'); //if you want to hide
$(".collapse").collapse('toggle'); //if you want toggle
$(".collapse").collapse('show'); //if you want to show
}