It provides access from within the component class to the ElementRef object for the <p> tag that has the bio template reference variable in the component's template view.
It indicates that the <p> tag is rendered as a child of the parent view that uses this component.
It makes the <p> tag in the template support content projection.
It makes the <p> tag visible in the final render. If the #bio was used in the template and the @ViewChild was not used in the class, then Angular would automatically hide the <p> tag that has #bio on it.
Q3. What is the difference between the paramMap and the queryParamMap on the ActivatedRoute class?
The paramMap is an object literal of the parameters in a route's URL path. The queryParamMap is an Observable of those same parameters.
The paramMap is an Observable that contains the parameter values that are part of a route's URL path. The queryParamMap is a method that takes in an array of keys and is used to find specific parameters in the paramMap.
paramMap is the legacy name from Angular 3. The new name is queryParamMap.
Both are Observables containing values from the requested route's URL string. The paramMap contains the parameter values that are in the URL path and the queryParamMap contains the URL query parameters.
Q4. Based on the following usage of the async pipe, and assuming the users class field is an Observable, how many subscriptions to the users Observable are being made?
<h2>Names</h2><div *ngFor="let user of users | async">{{ user.name }}</div><h2>Ages</h2><div *ngFor="let user of users | async">{{ user.age }}</div><h2>Genders</h2><div *ngFor="let user of users | async">{{ user.gender }}</div>
None. The async pipe does not subscribe automatically.
None. The template syntax is not correct.
Three. There is one for each async pipe.
One. The async pipe caches Observables by type internally.
Q11. What is the purpose of the valueChanges method on a FormControl?
It is used to configure what values are allowed for the control.
It is used to change the value of a control to a new value. You would call that method and pass in the new value for the form field. It even supports passing in an array of values that can be set over time.
It returns a Boolean based on if the value of the control is different from the value with which it was initialized.
It is an observable that emits every time the value of the control changes, so you can react to new values and make logic decisions at that time.
It provides a way to bind values to the itemTotalChanged class field, like so: <app-shopping-cart [itemTotalChanged]="newTotal"></app-shopping-cart>.
It provides a way to bind events to the itemTotalChanged class field, like so: <app-shopping-cart (itemTotalChanged)="logNewTotal($event)"></app-shopping-cart>.
It is simply a way to put a comment in front of a class field for documentation.
The ngIf is shorthand for the other example. When Angular processes that directive, it writes a div element to the DOM with the hidden property.
They are fundamentally the same.
The ngIf directive does not render the div in the DOM if the expression is false. The hidden property usage hides the div content in the browser viewport, but the div is still in the DOM.
The ngIf is valid, but the use of the hidden property is wrong and will throw an error.
Q17. Based on the following component, what template syntax would you use to bind the TitleCardComponent's titleText field to the h1 element title property?
providers that can be used to track the instances of components
built-in pipes that can be used in templates for DOM events
reserved named methods for components and directives that Angular will call during set times in its execution, and can be used to tap into those lifecycle moments
Q19. Pick the best description for this template syntax code:
<span>Boss: {{job?.bossName}}</span>
The ? is shorthand for the async pipe. The job value must be an Observable.
It is using the safe navigation operator (?) on the job field. If the job field is undefined, the access to the bossName will be ignored and no error will occur.
There is an error in the template syntax. The ? is not valid here.
It is diplaying the job value if it has one; otherwise it is displaying the bossName.
Q20. How would you configure a route definition for a UserDetailComponent that supports the URL path user/23 (where 23 represents the id of the requested user)?
They are setting the CalloutDirective.fontWeight field based on whether or not the mouse is over the DOM element. The HostListener then sets the font-weight CSS property to the fontWeight value.
They are setting up the directive to check the DOM element that it is on. If it has event bindings added for mouse enter and leave it will use this code. Otherwise, nothing will happen.
This is an incorrect use of HostListener and HostBinding. The HostListener and HostBinding decorators do not do anything on directives; they work only when used on components.
If the DOM element that this directive is placed on has the CSS property font-weight set on it, the mouseenter and mouseleave events will get raised.
Q22. What Angular template syntax can you use on this template-driven form field to access the field value and check for validation within the template markup?
They are the same. Both will result in a new instance of Logger that is bound to the FormattedLogger token.
The useClass syntax tells the injector to make a new instance of Logger and bind that instance to the FormattedLogger token. The useExisting syntax refers to an already existing object instance declared as Logger.
Both of them are wrong. A strong type cannot be used for useClass or useExisting.
They are the same. Both will result in the FormattedLogger token being an alias for the instance of Logger.
The <div> acts as a placeholder. If the product class field is "truthy," the <div> will get replaced by just the product.name value; if not, then nothing will be rendered.
The <div> will always be rendered, and if the product field is "truthy," the <div> element will contain the product.name value; otherwise, it will render the <div> element with no value in it.
It produces an error since ngIf is not a built-in structural directive.
If the product class field is "truthy," then the rendered DOM will include the <div> with the value of the product.name field. If it is not "truthy,' the rendered DOM will not contain the <div> element.
It provides a way to code the document structure of an Angular application. The @NgModule is a form of inline code commenting that gets ignored by the TypeScript compiler but will show up with special formatting in code editor applications.
It declares an Angular module named AppModule and makes it available for lazy loading throughout the application.
It declares an Angular module named AppModule that contains a bootstrapped component named AppComponent. Then it registers that module with Angular, so the app can start up.
Prior to loading the UserComponent, the router will subscribe to the Observable returned by a resolve method in the UserResolverService. This technique can be used to get preloaded data for a route.
After the route is done resolving, and the component is loaded and rendered, the UserResolverService will have a method named user run that will clean up any open data connections.
There is an error. The correct property name is onResolve.
The UserComponent will have a parameter in its constructor for user, and the router will handle injecting in a value for that from a call to a user method in the UserResolverService.
If any TabsComponent elements are added to the TabsListComponent template, they will get put into the <ng-content> element at runtime.
It creates TabComponent components in the TabsListComponent template when a TabsListComponent is instantiated.
It provides access from within the component class to any TabComponent components that were content projected into the <ng-content> for this component.
It restricts the allowed elements that can be put into a TabsListComponent element to allow only TabComponent elements.
Q33. When a service is provided for root and is also added to the provider's configuration for a lazy-loaded module, what instance of that service does the injector provide to constructors in the lazy-loaded module?
A new instance of that service is created when the module is lazy loaded.
Providing a service of the same type at a lazy-loaded module level is not allowed.
If an instance of the service has not been created at the root level yet. it will create one there and then use it.
A single instance of that service is always instantiated at the root and is the only one ever used, including within lazy modules.
Q34. What is the HostBinding decorator doing in this directive?
It is adding the CSS class named highlighted to any DOM element that has the appHighlight directive on it.
HostBinding does not do anything on directives, only on components.
It specifies that if the host element gets the highlighted class added to its class attribute, then the directive class field highlight will get set to true; and if it is not added on the host it will get set to false.
It is creating an inline style on the host element with a CSS property named highlight set to true.
Q35. In reactive forms, what Angular form class type is used on the native DOM <form> element to wire it up?
FormArray
FormControl
FormGroup
all of these answers
Q36. Assuming the username FormControl has been configured with a minLength validator, how can you set up an error display in the following reactive forms markup for the username field?
<inputtype="text"formControlName="username" #userName="ngModel" /><span *ngIf="userName.errors.minlength"> Username must be at least {{ userName.errors.minlength.requiredLength }} characters.
</span>
The RxJs pipe method is an alias for the subscribe method, so a call to getSettings will execute the get query. The retry operator is used to tell the pipe call to retry the get query three times.
It will produce an error at runtime because the pipe method is not available off of the Httpclient.get call.
Every single call to the getSettings method will result in the Httpclient making three total get requests to the settingsUrl, which is not ideal because there will always be two extra calls that are not needed. The retry operator should not be used in this manner.
When the result of the getSettings method is subscribed to, the HTTP GET call will be made; if it fails, it will be retried up to three times before it gives up and returns an error.
Q43. When a service requires some setup to initialize its default state through a method, how can you make sure that said method is invoked before the service gets injected anywhere?
Put the logic of that service method into the service constructor instead.
Use a factory provider at the root AppModule level that depends on the service to call that service method.
It is not possible to do it at the application start; you can do it only at a component level.
Instantiate an instance of the service at the global level (window scope) and then call that method.
The TestBed is required anytime you want to make use of a spy object in a unit test for an Angular provider.
The TestBed is being used to test a component's view.
The TestBed scaffolds an NgModule with two providers and handles any dependency injection. If any Angular class requests the DataService in its constructor, the TestBed will inject spy in that constructor.
The TestBed is configuring the test runner to tell it to only execute tests for the two providers listed in its providers array.
All other tests be ignored, including tests that assert results against one of these providers and a non-defined provider.Although it will work when multiple providers in this configuration are asserted against in a single test.
Q45. What is the primary difference between a component and a directive?
A component uses a selector metadata property and a directive does not.
A directive can be used for adding custom events to the DOM and a component cannot.
A component has a template and a directive does not.
Q46. What could you add to this directive class to allow the truncate length to be set during directive usage in markup?
@Directive({
selector: '[appTruncate]'
})
export class TruncateDirective {
. . .
}
// example of desired usage:<p [appTruncate]="10">Some very long text here</p>
@Input() appTruncate: number;
@Output() appTruncate;
constructor(maxLength: number) { }
Nothing. The directive selector cannot be used to pass in values to the directive.
Q48. Assuming the DataService has been registered in the providers for the application, which answer best describes what happens based on this component's constructor?
It declares that the OrderHistoryComponent will have its own version of a DataService and that it should never use any existing instances. The DataService would need to be instantiated within the class as a private field for this code to be complete and working.
When Angular creates a new instance of the OrderHistoryComponent, the injector will provide an instance of a DataService class to the component constructor's first argument. The constructor's dataService parameter will be used to set a private instance field with the same name on the instance.
It provides a way to do component testing only; the constructor has no usage in the actual run of the Angular application.
It enables the custom element that the component targets to have a custom property named dataService that can be used to bind an existing DataService instance to.
The FormControl for username is getting configured to exclude three validators from the validators that it is allowed to use.
The FormControl for username is getting configured to allow three possible validators to be used: required, maxLength, and a custom one named unique. To enable these validators, a validator directive would need to be put on the form fields in the markup.
Validation cannot be set up this way in reactive forms.
The FormControl for username is getting configured with three validators: the required and minLength validators that come from Angular, and a custom validator function named unique that checks for the value not equal to the string admin.
The InjectionToken adds an instance of the AppSettings to the root provider via the InjectionToken constructor call, making it automatically available to all NgModules, services and components throughout the Angular application without the need to inject it anywhere.
The InjectionToken is used to create a provider token for a non-class dependency. An Object literal can be provider as a value for the APP_SETTINGS dependency provider type that can then be injected into components, services, etc ..
The InjectionToken is used to create a dynamic decorator for the AppSettings that can be used on constructor parameters via an @AppSettings decorator.
This code has an error since you cannot use a TypeScript interface for the generic type on the InjectionToken
Q54. For the following template-driven forms example, what argument can be passed to the submit method in the click event to submit the data for the form?
[How do I call an Angular 2 pipe with multiple arguments?] (https://stackoverflow.com/questions/36816788/how-do-i-call-an-angular-2-pipe-with-multiple-arguments)
Q61. Which Angular CLI command would you run to generate a UsersComponent and add it to the SharedModule (in file shared.module.ts in your application)?
ng generate component --newModule=shared
ng generate component users --module=shared
ng generate component users --shared
ng generate component --add=shared
Q62. How can you rewrite this markup so the div container is not needed in the final DOM render
This code as an error since you cannot use a TypeScript interface for the generic type on the InjectionToken.
The InjectionToken is used to create a provider token for a non-class dependency. An object lieteral can be provided as a value for the APP_SETTINGS dependency provider type that can then be injected into components, services, etc.
The InjectionToken is adding an instance of the AppSettings to the roote provider via the the InjectionToken constructor call, making it auto available to all NgModules, services, and componentts throughtout the Angular application without the need to inject it anywhere.
The InjectionToken is used to create a dynamic decorator for the AppSettings that can be sed on constructor parameters via an @AppSettings decorator.
Q64. What Angular utilities, if any, are required to unit test a service with no constructor dependencies?
By.css() helper method is needed
A text fixture is required to run the service for the unit test.
None. A service can be instantiated and unit tested on its own.
The TestBed class is needed to instantiate the service.
Q65. What is the difference between the CanActivate and the CanLoad route guards?
CanActivate is used to check access. CanLoad is used to preload data for the route.
CanLoad is used at app startup to allow or deny routes to be added to the route table. CanActivate is used to manage access to routes at the time they are requested.
CanActivate and CanLoad do the exact same thing.
CanLoad prevents an entire NgModule from being delivered and loaded. CanActivate stops routing to a component in that NgModule, but that module is still loaded.
CanActivate vs Canload CanActivate prevents access on routes, CanLoad prevents lazy loading.
Q66. What is the outlet property used for in this router definition object?
it will locate all instancess of <document-box> in the DOM and inser a DocumentComponent element into them on route navigation.
It declares that the DocumentComponent can be used as a child to a <document-box> element in addition ot being routed to.
It us used to target a <router-outlet> element with the name attribute matching the string value as the location for the DocumentComponent to be rendered when routed to.
It is a source of power for the router. (definitely not the answer :P)
Q67. In this template syntax, every time the items property is changed (added to, removed from, etc.), the ngFor structural directive re-runs its logic for all DOM elements in the loop. What syntax can be used to make this more performant?
ng build --configuration=production --progress=false
It builds the Angular application, setting the build configuration to the "production" target specified in the angular.json file, and logs progress output to the console.
It builds the Angular application, setting the build configuration to the "production" target specified in the angular.json file, and watching files for changes.
It builds the Angular application, setting the build configuration to the "production" target specified in the angular.json file, and disables watching files for changes.
It builds the Angular application, setting the build configuration to the "production" target specified in the angular.json file, and prevents progress output to the console.
Q76. Which answer best explains the usage of ngModel in this template code?
<input [(ngModel)]="user.name" />
It is conditionally displaying the input element if the user.name property has a value.
It is the two-way data binding syntax. The input element value property will be bound to the user.name property, and the value change event for the form element will update the user.name property value.
There is a typo in the code. It should have only the square brackets.
It is binding the value of the user.name property to the input element's val property to set its initial value.
It indicates that the CustomValidatorDirective can be used on multiple form element types.
It allows for multiple instances of the CustomValidatorDirective to be instantiated. Without multi, the CustomValidatorDirective would be a singleton for the entire app.
It allows the registration of different providers for the single NG_VALIDATORS token. In this case, it is adding the CustomValidatorDirective to the list of form validators available.
It indicates that there will be multiple classes that handle the logic implementation for the custom validator.
Q80. Which Angular CLI command would you use to run your unit tests in a process that reruns your test suite on file changes?
ng test --single-run=false
ng test --watch-files
ng test --progress
ng test
Q81. What is the most common use for the ngOnDestory lifecle hook?
Remove dome elements from the components's view
All of these answers
Delete any injected services the
Unsubscribe from observables and detach
Q82. What NgModule decorator metadata property is leverage to allow other ....?
public
experts
Shared
declarations
Q83. What is the difference between the CanActivate and the CanLoad rout guards?
CanLoad Prevents an entire NGModule from being delivered and loaded. CanActivate stops routing to a component in that NgModule , but the module is still loaded.
CanActivate and CanLoad do the exact same thing.
CanActivate is used to check access. CanLoad is used to preload data for the route.
CanLoad is used at app startup to allow or deny routes to be added to the route table. CanActivate is used to manage access to routes at the time they are requested
Q84. With the following component class, what template syntax would you use in the template to display the result of calling the currentYear class function?