Angular: Empty Fiddle
http://angularjs.org/
by Wojciech Frącz
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
MyCtrl exists? {{ exists('MyCtrl') }} <br>
OtherCtrl exists? {{ exists('OtherCtrl') }} <br>
BadCtrl exists? {{ exists('BadCtrl') }}
</div>
JavaScript
var myApp = angular.module('myApp',[]);
function MyCtrl($scope, ControllerChecker) {
$scope.exists = function(name){ return ControllerChecker.exists(name); }
};
myApp.controller('OtherCtrl', function(){});
myApp.service('ControllerChecker', ['$controller', function($controller) {
return {
exists: function(controllerName) {
if(typeof window[controllerName] == 'function') {
return true;
}
try {
$controller(controllerName);
return true;
} catch (error) {
return !(error instanceof TypeError);
}
}
};
}]);