41⟩ Tell me what is factory method in AngularJS?
For creating the directive, factory method is used. It is invoked only once, when compiler matches the directive for the first time. By using $injector.invoke the factory method is invoked.
“AngularJS Developer related Frequently Asked Questions by expert members with job experience as AngularJS Developer. These questions and answers will help you strengthen your technical skills, prepare for the new job interview and quickly revise your concepts”
For creating the directive, factory method is used. It is invoked only once, when compiler matches the directive for the first time. By using $injector.invoke the factory method is invoked.
Changing .directive to .component to adapt to the new Angular 1.5 components
The $routeProvider is used to configure roots within an AngularJS application. It can be used to link a url with a corresponding HTML page or template, and a controller (if applicable).
AngularJS provides a supreme Dependency Injection mechanism. It provides following core components which can be injected into each other as dependencies.
value
factory
service
provider
constant
constants are used to pass values at config phase considering the fact that value cannot be used to be passed during config phase.
mainApp.constant("configParam", "constant value");
Following are the disadvantages of AngularJS.
☛ Not Secure − Being JavaScript only framework, application written in AngularJS are not safe. Server side authentication and authorization is must to keep an application secure.
☛ Not degradable − If your application user disables JavaScript then user will just see the basic page and nothing more.
Currency filter formats text in a currency format.
In below example, we've added currency filter to an expression returning number using pipe character. Here we've added currency filter to print fees using currency format.
Enter fees: <input type = "text" ng-model = "student.fees">
fees: {{student.fees | currency}}
Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts:
☛ Model − It is the lowest level of the pattern responsible for maintaining data.
☛ View − It is responsible for displaying all or a portion of the data to the user.
☛ Controller − It is a software Code that controls the interactions between the Model and View.
Scopes are objects that refer to the model. They act as glue between controller and view.
ng-hide directive hides a given control.
In below example, we've added ng-hide attribute to a HTML button and pass it a model. Then we've attached the model to a checkbox and can see the variation.
<input type = "checkbox" ng-model = "showHide2">Hide Button
<button ng-hide = "showHide2">Click Me!</button>
Create an AngularJS service that will hold the data and inject it inside of the controllers.
Using a service is the cleanest, fastest and easiest way to test.
However, there are couple of other ways to implement data sharing between controllers, like:
☛ Using events
☛ Using $parent, nextSibling, controllerAs, etc. to directly access the controllers
☛ Using the $rootScope to add the data on (not a good practice)
☛ The methods above are all correct, but are not the most efficient and easy to test.
In AngularJS services are the singleton objects or functions that are used for carrying out specific tasks. It holds some business logic and these function can be called as controllers, directive, filters and so on.
In the directives. DOM Manipulations should not exist in controllers, services or anywhere else but in directives.
Here is the list of AngularJS features that makes it the hottest tech for web dev.
☛ Data-binding – Handles synchronization of data across model, controllers, and view.
☛ Scope – Object representing the model, acts as a glue layer between controller and view.
☛ Controllers – JS functions bound to the scope object.
☛ Services – Substitutable objects that are wired together using dependency injection. e.g. $location service.
☛ Filters – Formats the value of an expression for displaying to the user. e.g. uppercase, lowercase.
☛ Directives – These are extended HTML attributes start with the “ng-” prefix. e.g. ng-app directive used to initialize the angular app.
☛ Templates – HTML code including AngularJS specific elements and attributes.
☛ Routing – It’s an approach to switch views.
☛ MVC pattern – A design pattern made up of three parts.
☛ Model – Represents data, could be static data from a JSON file or dynamic data from a database.
☛ View – Renders data for the user.
☛ Controller – Gives control over the model and view for collating information to the user.
☛ Deep linking – Enables the encoding of the application state in the URL and vice versa.
☛ Dependency injection – A design pattern to let the components injected into each other as dependencies.
☛ Registering Callbacks: There is no need to register callbacks . This makes your code simple and easy to debug.
☛ Control HTML DOM programmatically: All the application that are created using Angular never have to manipulate the DOM although it can be done if it is required
☛ Transfer data to and from the UI: AngularJS helps to eliminate almost all of the boiler plate like validating the form, displaying validation errors, returning to an internal model and so on which occurs due to flow of marshalling data
☛ No initilization code: With AngularJS you can bootstrap your app easily using services, which auto-injected into your application in Guice like dependency injection style
MVC is a common design pattern used in developing software that consists of three parts: the model, view, and controller. The model is the lowest level of the pattern and is responsible for maintaining data. The view is the part of the application that is presented to the user. The controller is the code that governs all interactions between the model and the view.
An injector is a service locator, used to retrieve object instance as defined by provider, instantiate types, invoke methods, and load modules.
Using AngularJS, we can embed HTML pages within a HTML page using ng-include directive.
<div ng-app = "" ng-controller = "studentController">
<div ng-include = "'main.htm'"></div>
<div ng-include = "'subjects.htm'"></div>
</div>
AngularJS was designed to highlight the power of dependency injection, a software design pattern that places an emphasis on giving components their dependencies instead of hard coding them within the component. For example, if you had a controller that needed to access a list of customers, you would store the actual list of customers in a service that can be injected into the controller instead of hardcoding the list of customers into the code of the controller itself. In AngularJS you can inject values, factories, services, providers, and constants.
Controllers are JavaScript functions that are bound to a particular scope. They are the prime actors in AngularJS framework and carry functions to operate on data and decide which view is to be updated to show the updated model based data.