In AngularJS, I\'ve noticed that a controller is injected with $element
, which is a JQuery/JQLite wrapper of the element the controller is controlling. For exam
Whether you inject $element or not, the controller's scope is bound on that element.
angular.element('#element-with-controller').scope();
Angular revolves around directives. It's what glues things together in the MVC. And if you think about it, ng-controller, is a directive itself.
I guess this can come in handy when you're using a single controller for multiple directives.
.controller('MyController', function($scope, $element){
$scope.doSomething = function(){
// do something with $element...
}
})
.directive('myDirective1', function(){
return {
controller: 'MyController'
}
})
.directive('myDirective2', function(){
return {
controller: 'MyController'
}
})
Each directive will have a new instance of the assigned controller, but basically share it's properties, dependencies.
I wrote a form handler controller once, for registration/login/contactus, etc.