Although this is probably a more general JS question than Angular, the same theories still apply. You should wrap things in try / catch / finally blocks and throw errors if necessary.
function Blah(isRequired, isOptional){
var X = 7, Y = 0;
try{
Y = X * isRequired;
}
catch(err){
throw new Error('You must provide a value for "isRequired"');
}
finally{
}
}
If you want to show the log, you can use the Angular shell to write to the console. This will prevent errors when using IE and try to write to console.debug by writing them instead of console.log.
Setup:
myapp.config(['$logProvider', function($logProvider){
$logProvider.debugEnabled(true);
})
.run(['$rootScope', '$log',
function ($rootScope, $log) {
'use strict';
$rootScope.$log = $log;
}]);
Using:
$scope.$log.log(message);
$scope.$log.debug(message);
$scope.$log.info(message);
$scope.$log.warn(message);
$scope.$log.error(message);
/ , . - .
<div data-log-enabled my-directive>
</div>
:
link: function(scope, element, attributes){
if(attributes.logEnabled){
scope.$log.debug('blah');
}
}
, , , , .