*
Previous AngularJS-QA-10 AngularJS-QA-12 Next

AngularJS Question Answers - 11

Question: With options on page load how you can initialize a select box ?
Answer: You can initialize a select box with options on page load by using ng-init directive
<div ng-app ng-init="options=[{ value: '1', desc: 'Option 1'},
                              { value: '2', desc: 'Option 2'},
                              { value: '3', desc: 'Option 3'}]">
                 <select ng-model="selectedOption"
                 ng-options="o.desc for o in options"></select>
</div>
Question: What is the purpose of the directive 'ng-csp'?
Answer: ng-csp: Changes the content security policy.
Question: What is the purpose of the directive 'ng-cut'?
Answer: ng-cut: Specifies a behavior on cut events.
Question: What is the purpose of the directive 'ng-dblclick'?
Answer: ng-dblclick: Specifies a behavior on double-click events.
Question: How to reset a $interval()?
Answer: To reset a $interval, assign the result of the function to a variable and then call the .cancel() function.
var customTimeout = $timeout(function () {
  //  code
}, 45);
$interval.cancel(customTimeout); 
Question: How to react on model changes to trigger some further action? For instance, input text field email and you want to trigger or execute some code as soon as a user starts to type in their email.
Answer: To do this use $watch function in our controller.
function MyCtrl($scope) {
 $scope.email = "";

 $scope.$watch("email", function(newValue, oldValue) {
  if ($scope.email.length > 0) {
   console.log("User has started writing into email");
  }
 });
} 
Question: What is the purpose of the directive 'ng-controller'?
Answer: ng-controller: Defines the controller object for an application.
Question: How to reset a $timeout?
Answer: To reset a timeout, assign the result of the function to a variable and then call the .cancel() function.
var customTimeout = $timeout(function () {
  //  code
}, 45);
$timeout.cancel(customTimeout); 
Question: How to implement application-wide exception handling in AngularJS?
Answer: Use a built-in error handler service called '$exceptionHandler' which can easily be overriden.
myApp.factory('$exceptionHandler', function($log, ErrorService) {
    return function(exception, cause) {        
        if (console) {
            $log.error(exception);
            $log.error(cause);
        }
        ErrorService.send(exception, cause);
    };
});
This is very useful for sending errors to third party error logging services or helpdesk applications. Errors trapped inside of event callbacks are not propagated to this handler, but can manually be relayed to this handler by calling $exceptionHandler(e) from within a try catch block.
Question: When to use the an attribute while creating a directive in AngularJS?
Answer: Use an attribute when you are decorating an existing element with new functionality.
Back to Index
Previous AngularJS-QA-10 AngularJS-QA-12 Next
*
*