Don't forget to add <router-outlet></router-outlet> if you do routing of parent or child component. Like for tab menu.
Thursday, June 21, 2018
Thursday, June 14, 2018
Textbox Character Count in Angular 2, 4, 5, 6
import { | |
| Directive, | |
| ElementRef, | |
| HostListener, | |
| Input, | |
| Renderer2, | |
| AfterViewInit, | |
| } from '@angular/core'; | |
| /** | |
| * Define the CharCount Directive. | |
| */ | |
| @Directive({ | |
| selector: '[charCount]', | |
| }) | |
| /** | |
| * CharCount Class. | |
| */ | |
| export class CharCount implements AfterViewInit { | |
| // Default character limit if none is supplied. | |
| private _defaultLimit: number = 200; | |
| // The character limit input variable. | |
| @Input('charCount') _limit: number; | |
| /** | |
| * Class constructor. | |
| * | |
| * @param { ElementRef } _elRef | |
| * An ElementRef object instance. | |
| * @param { Renderer2 } _renderer2 | |
| * A Renderer object instance. | |
| */ | |
| constructor(private _elRef: ElementRef, private _renderer2: Renderer2) { } | |
| @HostListener('keyup') onKeyUp() { | |
| // When the field on the host is updated. Update the count display. | |
| let count: any = this._elRef.nativeElement.value.length; | |
| this._renderer2.setValue(this._elRef.nativeElement.nextElementSibling.firstChild, String(count) + '/' + this._limit); | |
| } | |
| /** | |
| * Implements the AfterViewInit lifecycle hook. | |
| */ | |
| ngAfterViewInit() { | |
| // If no character limit was supplied via input, then use the default limit. | |
| this._limit = this._limit || this._defaultLimit; | |
| // A html nativeElement (small) to display the character count. | |
| const countElement = this._renderer2.createElement('small'); | |
| const elementValue = this._renderer2.createText( this._elRef.nativeElement.value.length + '/' + String(this._limit) ); | |
| this._renderer2.appendChild(countElement, elementValue); | |
| this._renderer2.appendChild(this._elRef.nativeElement.parentElement, countElement); | |
| this._renderer2.addClass(this._elRef.nativeElement.nextElementSibling, 'max-words'); | |
| } | |
| } |
Friday, August 19, 2016
Difference between ng-bind and expression
ng-bind not evaluate the html till the page is loading. If we use the expression {{expression}} then the html is evaluated and the values are flicked momentarily. Alternate to resolve this situation we can use ng-cloak. And put this css into css file.
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}
Thursday, August 11, 2016
Provider in Angular JS
All the web applications that we build uses Object that collaborate to get things done. These objects need to be instantiated.In angular, these objects are instantiated and wired by injector service.
Injector creates two types of objects - services and specialize objects.
Services objects are those API's that are defined by developer.
Specialized objects are defined specifically by Angular API, for e.g. controllers, directives, filters or animations.
Now, we have to specify that how to create these objects. There are five ways to create these objects.
* Value * Factory * Services * Constant and last but not the least Provider.
So, Lets discuss all one by one. I'll start by provider first.
Provider: Provider is the core of all type. All other are just the extension over it.
Provider is the implementation of $get method. This method is same like as we use in Factory.
Provider is used where we set the application-wide configuration that must be instantiated before the application start.
When angular application is going to bootstrap, before creating all the services, angular instantiates all providers.
Provider's are accessible during configuration phase, because services are not accessible at that time.
After completion of configuration phase, interaction with provider is not allowed and service instantiation process gets started.
Injector creates two types of objects - services and specialize objects.
Services objects are those API's that are defined by developer.
Specialized objects are defined specifically by Angular API, for e.g. controllers, directives, filters or animations.
Now, we have to specify that how to create these objects. There are five ways to create these objects.
* Value * Factory * Services * Constant and last but not the least Provider.
So, Lets discuss all one by one. I'll start by provider first.
Provider: Provider is the core of all type. All other are just the extension over it.
Provider is the implementation of $get method. This method is same like as we use in Factory.
Provider is used where we set the application-wide configuration that must be instantiated before the application start.
When angular application is going to bootstrap, before creating all the services, angular instantiates all providers.
Provider's are accessible during configuration phase, because services are not accessible at that time.
After completion of configuration phase, interaction with provider is not allowed and service instantiation process gets started.
CRUD operation with XML file
This is the XML document:
<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
<Table>
<Username>Ashutosh</Name>
<EMail>test@xyz.com</Age>
</Table>
</NewDataSet>
To add new node in XML document.
void AddUser()
<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
<Table>
<Username>Ashutosh</Name>
<EMail>test@xyz.com</Age>
</Table>
</NewDataSet>
To add new node in XML document.
void AddUser()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("User.xml");
XmlElement subRoot = xmlDoc.CreateElement("Table");
//UserName
XmlElement appendedElementUsername = xmlDoc.CreateElement("Name");
XmlText xmlTextUserName = xmlDoc.CreateTextNode("Ashutosh");
appendedElementUsername.AppendChild(xmlTextUserName);
subRoot.AppendChild(appendedElementUsername);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Age
XmlElement appendedElementAge = xmlDoc.CreateElement("Age");
XmlText xmlTextAge = xmlDoc.CreateTextNode(25);
appendedElementEmail.AppendChild(xmlTextAge );
subRoot.AppendChild(appendedElementAge );
xmlDoc.DocumentElement.AppendChild(subRoot);
xmlDoc.Save("User.xml");
}
To Update a particular node in XML document.
void UpdateUser()
{
XDocument XMLDoc = XDocument.Load(XMLfile);
IEnumerable<XElement> elem_list = from elem in XMLDoc.Descendants("Table")
where elem.Element("Name").Value == "Ashutosh"
select elem;
XElement[] elem_array = elem_list.ToArray();
foreach (XElement elem in elem_array)
{
elem.Element("Name").Value = "Ashutosh Sharma";
elem.Element("Age").Value = "26";
}
XMLDoc.Save("User.xml");
}
To Delete a particular node from XML document:
void DeleteUser()
{
XDocument xmldoc= XDocument.Load(XMLfile);
XElement user= xmloc.Descendants("Table").FirstOrDefault(p => p.Element("Name").Value == "Ashutosh");
if (user!= null)
{
user.Remove();
xmldoc.Save("User.xml");
}
}
To Update a particular node in XML document.
void UpdateUser()
{
XDocument XMLDoc = XDocument.Load(XMLfile);
IEnumerable<XElement> elem_list = from elem in XMLDoc.Descendants("Table")
where elem.Element("Name").Value == "Ashutosh"
select elem;
XElement[] elem_array = elem_list.ToArray();
foreach (XElement elem in elem_array)
{
elem.Element("Name").Value = "Ashutosh Sharma";
elem.Element("Age").Value = "26";
}
XMLDoc.Save("User.xml");
}
To Delete a particular node from XML document:
void DeleteUser()
{
XDocument xmldoc= XDocument.Load(XMLfile);
XElement user= xmloc.Descendants("Table").FirstOrDefault(p => p.Element("Name").Value == "Ashutosh");
if (user!= null)
{
user.Remove();
xmldoc.Save("User.xml");
}
}
Monday, August 8, 2016
Content Negotiation in ASP.NET Web API
Content negotiation is the process that describe the in which format the response is provide by Web API according to the request.
The HTTP specification (RFC 2616) defines content negotiation as “the process of selecting the best representation for a given response when there are multiple representations available.” The primary mechanism for content negotiation in HTTP are these request headers:
The HTTP specification (RFC 2616) defines content negotiation as “the process of selecting the best representation for a given response when there are multiple representations available.” The primary mechanism for content negotiation in HTTP are these request headers:
- Accept:Which media types are acceptable for the response, such as “application/json,” “application/xml,” or a custom media type such as "application/vnd.example+xml"
- Accept-Charset: Which character sets are acceptable, such as UTF-8 or ISO 8859-1.
- Accept-Encoding: Which content encodings are acceptable, such as gzip.
- Accept-Language: The preferred natural language, such as “en-us”.
The server looks for above HTTP request details.
Advantage or Benefits of TypeScript
- Static type checking
- Similar to language like C#, JAVA, Scala
- Support ES6
- It offers support for the latest ECMAScript for future proposals.
- It support almost all the popular Javascript libraries like JQuery, Angular JS, Angular 2 etc.
- Typescript is the superset of Javascript.
- Its support typings that is highly productive.
- With the help of type checking we can static verify our code.
- Provide better intellisence
- Support class, interface and modules
Subscribe to:
Posts (Atom)