Hey everyone,
I've been attempting to create an accordion using a directive in Angular, but I keep encountering this error message in the console.
Error: [ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=CustomDirectivesController&p1=not%20a%20function%2C%20got%20undefined
at Error (native)
at file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:6:416
at Nb (file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:19:417)
at ob (file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:20:1)
at file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:75:177
at file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:57:112
at r (file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:7:408)
at I (file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:56:496)
at g (file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:51:299)
at g (file:///Users/Marcelo/Desktop/Online-Colleges/capi/view/scripts/angular/angular.min.js:51:316)
Here is my complete AngularJS code:
customDirectives = angular.module('customDirectives', []);
customDirectives.directive('customCollapse', function () {
return {
require: '?ngModel',
scope:{
ngModel: '='
},
restrict: 'A',
template: '<div class="panel-group" id="{{panelId}}">\
<div class="panel panel-default" ng-repeat-start="item in ngModel">\
<div class="panel-heading">\
<h4 class="panel-title">\
<a ng-click="toggleCollapsedStates($index)" href="#{{panelBaseId}}-{{$index}}">{{item.title}}</a>\
</h4>\
</div>\
<div id="{{panelBaseId}}-{{$index}}" data-parent="#{{panelId}}" class="panel-collapse collapse">\
<div class="panel-body">{{item.content}}</div>\
</div>\
</div>\
<div ng-repeat-end></div>\
</div>',
link: function (scope, el, attrs) {
scope.panelBaseId = attrs.collapsePanelBodyId;
scope.panelId = attrs.collapsePanelId;
$(document).ready(function(){
angular.forEach(scope.ngModel, function(value, key){
if (value.collapsed)
{
$("#" + scope.panelBaseId + "-" + key).collapse('show');
}
});
});
scope.toggleCollapsedStates = function(ind){
angular.forEach(scope.ngModel, function(value, key){
if (key == ind)
{
scope.ngModel[key].collapsed = !scope.ngModel[key].collapsed;
$("#" + scope.panelBaseId + "-" + ind).collapse('toggle');
}
else
scope.ngModel[key].collapsed = false;
});
}
}
};
});
angular.module('CustomComponents', ['customDirectives']);
function CustomDirectivesController($scope)
{
$scope.collapseData = [
{
title: "Collapse Group Item Title 1",
content: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.",
collapsed: true
},
{
title: "Collapse Group Item Title 2",
content: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.",
collapsed: false
},
{
title: "Collapse Group Item Title 2",
content: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.",
collapsed: false
}
];
}
Additionally, here is the HTML snippet:
<div ng-app="customDirectives">
<div ng-controller="CustomDirectivesController">
<div custom-collapse ng-model="collapseData" collapse-panel-id="collapse-panel" collapse-panel-body-id="collapse-panel-body"></div>
</div>
</div>
I have reviewed similar questions related to this issue, and it seems like everything is set up correctly. Can anyone help me identify where I might be going wrong?
If there is someone who can provide insight into what could be causing this problem, I would greatly appreciate it.