Tuesday, February 6, 2018

Create Your First Angular App: Storing and Accessing Data

In the first tutorial of the series, we learned how to get started in order to create an Angular app. After successfully completing that tutorial, you should now have your first functioning Angular app with the heading 'Fun Facts About Countries'. Before creating any components that can be rendered on the screen, we will create some classes and define some functions that make those components useful.

In this tutorial, our focus will be on creating a Country class which will contain different properties whose value we want to display to the user. We will then create another file named country-data.ts. This file will contain information about all the countries in our app. Our third file will be named country.service.ts. The name may sound fancy, but the file will just contain a CountryService class with all the necessary functionality to retrieve and sort the information provided by the file country-data.ts.

Creating a Country Class

Inside the src/app folder of your Angular app, create a file named country.ts. Add the following code inside it.

The above TypeScript code defines the Country class with six different properties to store information about different countries. The name of the country, its capital, and its currency are stored as a string. However, its area, population, and GDP are stored as a number. We will be importing the Country class in a lot of places, so I have added the export keyword before the class definition.

Creating an Array of Countries

The next step includes creating a country-data.ts file to store the information about all the countries as an array of Country objects. We will be importing the Country class in this file and then exporting a const named COUNTRIES which stores an array of country objects. 

Here is the code for country-data.ts. Just like country.ts, you have to create this file inside the src/app folder.

The first line in this file imports the Country class from the country.ts file located in the same directory. If you remove this line from the file, TypeScript will give you the following error:

Without the import statement, TypeScript will have no idea what an array of type Country means. So make sure that you have imported the right class and specified the location of country.ts correctly.

After importing the Country class, we go ahead and create an array of Country objects. We will be importing this array of countries for use within other files, so we add an export keyword to this array as well. Currently, there are five different Country objects in the array. Each of these five objects provides key-value pairs that list the name of a property and its value for a particular object or country.

If you try to add an additional property to the array which has not been declared inside the Country class definition, you will get the following error:

In this case, I was trying to store the name of the president as a string inside a property named president. Since no such property was declared, we got an error. Sometimes, you might want to specify a property only for particular objects and not for others. In such cases, you can mark the property optional inside the class definition. I have discussed it in more detail in a tutorial which covers TypeScript Interfaces.

For now, just make sure that the names of all the properties match the names inside the class definition. Also make sure that the value of each property has the same type as declared in the class definition.

Creating a CountryService Class

After creating our Country class and COUNTRIES array, we can now finally write some functions to process the country data. We will need to import both the Country class and the COUNTRIES array inside our service file. The file will need to import the COUNTRIES array in order to have access to the data. Similarly, the file will have to import the Country class in order to make sense of the data inside the COUNTRIES array.

We will also be importing other dependencies like Injectable from Angular core to make our CountryService class available for the Injector to inject in other components.

Once your app grows in size, different modules will need to communicate with each other. Let's say that ModuleA requires ModuleB in order to work properly. In such cases, we would call ModuleB a dependency of ModuleA

Simply importing the module we need into another file works most of the time. However, sometimes we need to decide if we should create a single instance of classes from ModuleB that will be used by the whole app or if we should create a new instance every time the module is used. In our case, we will be injecting a single instance of our CountryService class throughout the app.

Here is the code for country.service.ts:

An @injectable decorator is used to identify a service class that might require injected dependencies. However, adding the @injectable to service classes is a required coding style, so we do it anyway.

After that, we write different methods for the class which take the COUNTRIES array and either return it directly or sort it using certain criteria and then return a part of the array. 

The getCountries() method is expected to return all the Country objects, and so it returns the whole COUNTRIES array without making any modifications to it.

The getPopulatedCountries() takes the COUNTRIES array and sorts it in descending order based on the population of different countries. We then use the Array.slice() method in order to return the first three countries (with indices 0, 1, and 2) from the array. The getLargestCountries() and getGDPCountries() methods work in a similar fashion.

The getCountry() method takes a name as its argument and returns the country object whose name property has the same value as the supplied name argument.

Including CountryService in app.module.ts

A service that you create is just a class in Angular until you have registered it with an Angular dependency injector. An Angular injector will be the one responsible for creating service instance(s) and injecting them into different classes which need that service. We need to register a service with a provider before the injector can create that service.

There are two common ways to register any service: using a @Component provider or using the @NgModule provider. Using the @Component provider makes sense when you want to restrict the access of a service to a particular component and all its nested components. Using the @NgModule provider makes sense when you want multiple components to have access to the service.

In our case, we will be using CountryService with multiple components of our app. This means that we should register it with the @NgModule provider once, instead of registering it separately with the @Component provider of each component. 

Currently, your app.module.ts file should look like this:

Add an import statement to the app.module.ts file and add the service to the @NgModule providers array. After making these changes, your app.module.ts file should look like this:

The CountryService class will now be available to all the components that we create for our app.

Final Thoughts

Successfully creating three files named country.ts, country-data.ts, and country.service.ts concludes the second tutorial of this series.

The country.ts file is used to create a Country class with different properties like name, currency, population, area, etc. The country-data.ts file is used to store an array of country objects which have information about different countries. The country.service.ts file contains a service class with different methods to access the country data from the COUNTRIES array. Writing all these methods separately inside a service class allows us to access them inside different app components from a central location.

In the last section, we registered our service with the @NgModule provider to make it available for use inside different components.

The next tutorial will show you how to create three different components in your app to display country details and a list of countries.


by Monty Shokeen via Envato Tuts+ Code

7 Twitter Hashtag Research Tools for Marketers

Do you use hashtags for marketing campaigns on Twitter? Looking for hashtag tools to help improve your use of hashtags? In this article, you’ll find seven hashtag tools for researching and reporting on Twitter hashtags. Why Is Researching Twitter Hashtags Important? Twitter users often follow hashtags that pertain to their interests, so social media marketers [...]

This post 7 Twitter Hashtag Research Tools for Marketers first appeared on .
- Your Guide to the Social Media Jungle


by Lindsay Bartels via

Akademi™

Akademi™ is a design studio with dig­i­tal focus. We want to make the modern world more acces­sible with sus­tain­a­ble, func­tion­al and delightful experiences.
by via Awwwards - Sites of the day

11 Reasons Why No One is Watching Your Video Content - #Infographic

Video is a powerful medium for promotional activities of businesses. As a marketer, you might have created several videos for the promotional activities of your business. Though such videos are of good quality you may not get enough viewers for it. What will be the reason for this? There may be...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

Announcing Versioning 2.0

Versioning was something we started waaaay back in 2014. The idea was, the internet is exhausting, particularly for devs, designers and web folk like you. Why not get someone (me) to keep up with all of that stuff for you, and share the best content on a daily basis? It turns out, people (you) wanted that, so I’ve written the daily newsletter ever since.

BUT I also had another, full-time role at SitePoint, so Versioning was squeezed in where it fit.

Now, I’ve stopped squeezing. Versioning is my entire job, and I’m entirely focused on making the newsletter as useful and amazing as possible, so that it’s worthwhile subscribing to. I have a whole day to produce every edition of Versioning, so I can do things like widen the range of sources, widen the range of topics covered based on feedback, fully explore each link, and produce more and more varied content for y’all. The core newsletter will always be there, but additional updates on specific subjects or areas, canonical, updated posts and interviews can happen too. Down the line, the sky’s the limit.

We could’ve financed this move by adding more, and maybe more disruptive, advertising and sponsorships, but this lets the content be the focus. You’ll get stuff that’ll make your day (or your week, or your life) better, and nothing else.

Instead, we’re relaunching the newsletter as a paid subscription product. For a small monthly or annual fee, you can become a Versioning Member and get the new, improved daily Versioning. This will come to your inbox and will also be viewable online. No ads, better links, better commentary and additional content and products (forums, for example, are on the horizon) where they make sense.

You can also sign up to receive a weekly update - that I’m planning on making very awesome in itself - plus periodic free updates. You can see some of those here.

To do this we have partnered with a company called Substack [substack]. They’re a start-up entirely dedicated to making subscription publishing work for content producers. (Btw, they’re co-founded by a New Zealander, like me, because Kiwis are amazing.)

Sign up here for the special special launch price of US $5 per month, or you can get an annual subscription for $50. Once you purchase a subscription under these special prices, they’ll renew at that rate as long as you’re still subscribed. This deal runs until March 5 and the price returns to the standard rate of $7/month or $70/year.

This is a new thing for us, and for the content industry in general, so we’re pumped! I hope you join us!

Continue reading %Announcing Versioning 2.0%


by Adam Roberts via SitePoint

Monday, February 5, 2018

Why You Should Focus Your Marketing Efforts On Mobile Ads and Social Media - #infographic

If you are a professional in today’s marketing industry, there’s a good chance you and your team have spent some time studying social media and mobile advertising. It’s for good reason too. Social media platforms and mobile advertising are a perfect combination that results in a higher return on...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

Why Major Media Companies Trust WordPress - #Infographic

WordPress makes publishing easy for everyone, from individual bloggers to the largest media companies on the web. In this infographic, you look at which major media companies trust WordPress and why.

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World