Below is the syntax of for each loop:
angular.ForEach(students,function(value,key)
{
//some code
}
To parse JSON, we can use any loop, but I would use for each loop because it will minimize my code by eliminating the need to store the length of a JSON in a variable.
AngularJs is an MVC based framework, where Model for a controller contains data, the controller for a view contains the logic to manipulate that data, and the view is the HTML that displays the data.
A $scope can be considered as a model, whereas the functions written in angular controller modifies the $scope and HTML display the value of the scope variable.
Two-way binding means that when data in the view is changed the underlying model gets updated automatically and when a model from the controller is changed the view gets updated.
No, there can't be more than one ng-apps for a single AngularJS application.
The ng-app directive conveys AngularJS application that it is the root element. In your HTML document, you can have a single ng-app directive only. In case of more than one ng-app directives, the first appearance will be used.
$scope is a model for a controller and helps the controller in interacting with the view.
(This is a super short answer to this question, but it is complete in every sense. Try not to put any additional angular terms).
Currency, lowercase, uppercase, number, date are few inbuilt angular filters.
{{nameOfStudent|uppercase}}
With AngularJS we can create our own filters. This can be done by associating the filter to our module. These types of filters are called custom filters.
Below is the code to count the number of elements in the string by using filter:
angular.module('myCountFilterApp', [])
.filter('count',function()
{
return(function(input)
{
var out=[];
out=input.split(',');
return out.length;
})
});
In the above example, if the string is "21, 34, 45" then output after applying filter will be 3.
Ng-if doesn’t render the portion of DOM element on which it is associated if the specified condition is not met whereas ng-show renders the DOM element but set its CSS property of display to none if the specified condition is not met.
The purpose of $watch is to keep track of the old and new value of the watched expression. Below is the code of using $watch.
$scope.$watch("checkInDate", function (newValue, oldValue) {
console.log("I've changed : ", newValue);
});
$rootScope helps in communication between different controllers of an application. AngularJS can have only one rootScope for an app.
$http({
method: "POST",
url: "URL",
data: JSON.stringify(value),
type: 'POST',
contentType: 'application/json; charset=utf-8'
}).then(function (response)
{
// success action
});
Form action should be used at a place where the server-side method takes the control to some other view in other word leads to redirection whereas HTTP request should be used where the server method returns some data.
Find index returns the position of an element in an object. If the requested element is not found then -1 is returned.
var index = $scope.items.findIndex(record => record.date =='2018-12-12');
In the above code, index of the object is returned where item.date=2018-12-12.
Ng-init is used in a scenario where we want some action to be done before the initialization of a portion of the DOM element.
Yes, we can do that by injecting PHP in the required place.
$scope.name='';
This will work only if you are using PHP to render the HTML and the above javascript is writter in tag inside the php file.
{{ Somevalue|lowercase|uppercase}}
Pipe operator in AngularJS represents filters that are used on the expression. The preference order is from left to right. So, the result of the above expression would be SOMEVALUE.
{{hotel.name}}
Here, setFinalFilter is a custom filter used on the hotels object. The result would display the name of filtered hotels in ascending order of their minPrice.
Services in AngularJS are objects which are used to communicate within entire applications.
app.service('sharedData', function () {
//methods to get and set variable
});
Dependency injection is a powerful design pattern that allows separating the concerns of different components in an application and provides a way to inject the dependent component into the client component.
Consider the below code:
myApp.controller('myController', function ($scope, $http, $location)
{
//logic
});
Here, a controller is declared with its dependencies.
.$http, $location are all services which are injected into the controller as a dependent entity. All of them have some independent specific task associated with it. MyController does not need to create their instance, but it can directly use them.
The syntax for creating new data object:
$scope.newDate=new Date();