0

I want to use UI Bootstrap Collapse in my custom directive <collapse>

However, I get this Error: [ng:areq] Argument 'CollapseDemoCtrl' is not a function, got undefined

Here is my Plunkr

What am I doing wrong?

3 Answers 3

1

Remove ng-controller from template. Define controller inline:

controller: ['$scope', function($scope){
    $scope.isCollapsed = false;
  }]

Plunk

Another option is to define controller:

.controller('CollapseDemoCtrl', function($scope){
  $scope.isCollapsed = false;
});

and refer to it in directive: controller: 'CollapseDemoCtrl'

Plunk

Sign up to request clarification or add additional context in comments.

4 Comments

do I really need to define the controller inline? Is there no other way?
It's all cool, but I don't understand why my approach isn't working. Why won't it pass my controller to the directive?
You did not define a controller, just added a function.
can you post the plunk from your comment as an answer, cause this is the one providing an answer to my question?
1

angular.module('myApp.collapse', []);
angular.module('myApp', [
  'ngAnimate',
  'myApp.collapse',
  'ui.bootstrap'
]);

(function(){
  'use strict';
  
angular.module('myApp.collapse',[])
.controller('CollapseDemoCtrl', CollapseDemoCtrl)
.directive('collapse', function(){
    return {
      restrict: 'E',
      templateUrl: 'collapse.html',
      controller: 'CollapseDemoCtrl' 
    };
  });
  function CollapseDemoCtrl($scope){
    $scope.isCollapsed = false;
  }

  
})();
<!doctype html>
<html ng-app="myApp">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.1.0.js"></script>
    <script src="app.js"></script>
    <script src="collapse-module.js"></script>
    <script src="collapse-directive.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

    <collapse></collapse>
    
  </body>
</html>


<div>
	<button type="button" class="btn btn-default" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button>
	<hr>
	<div uib-collapse="isCollapsed">
		<div class="well well-lg">Some content</div>
	</div>
</div>

4 Comments

(function(){ 'use strict'; var app = angular.module('myApp.collapse',[]); app.controller('CollapseDemoCtrl', CollapseDemoCtrl); app.directive('collapse', function(){ return { restrict: 'E', templateUrl: 'collapse.html', controller: 'CollapseDemoCtrl' }; }); function CollapseDemoCtrl($scope){ $scope.isCollapsed = false; } })();
your not define the controller. u need to define like this(app.controller('CollapseDemoCtrl', CollapseDemoCtrl);)
old : controller: CollapseDemoCtrl now controller: 'CollapseDemoCtrl'
your answer helped, thank you. Although Artem answered my question most precisely.
0

angular.module('myApp.collapse', []);


angular.module('myApp', [
  'ngAnimate',
  'myApp.collapse',
  'ui.bootstrap'
]);


(function(){
  'use strict';
  
  angular.module('myApp.collapse').
  directive('collapse',['$http', function($http){
   
   
   
    console.log("Test")
    return {
      restrict: 'E',
      templateUrl: 'collapse.html',
      controller: function ($scope) {
     $scope.isCollapsed = false; 
      }
    }; 
   
}]); 


		
   
  
})();
<!doctype html>
<html ng-app="myApp">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.1.0.js"></script>
    <script src="app.js"></script>
    <script src="collapse-module.js"></script>
    <script src="collapse-directive.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

    <collapse></collapse>
    
  </body>
</html>



<div>
	<button type="button" class="btn btn-default" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button>
	<hr>
	<div uib-collapse="isCollapsed">
		<div class="well well-lg">Some content</div>
	</div>
</div> 

3 Comments

Why did you need '$http'?
now not need. In future if any json or server call is thr u can use it.
Ok, but this still doesn't answer the error about the controller function being not a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.