{"id":1445,"date":"2016-10-05T15:27:42","date_gmt":"2016-10-05T15:27:42","guid":{"rendered":"https:\/\/ionic.io\/blog\/?p=1445"},"modified":"2017-01-09T06:58:14","modified_gmt":"2017-01-09T06:58:14","slug":"working-with-data-services-vs-nav-params","status":"publish","type":"post","link":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params","title":{"rendered":"Working with Data: Services and Nav Params"},"content":{"rendered":"<p>It&#8217;s pretty much unavoidable that we&#8217;re going to need to pass data around our app at some point. Why? Because apps are made up of a whole bunch of views and user interactions and&#8230;stuff, none of which tend to exist in isolation. Whether we&#8217;re talking about data pulled in via a REST API, collected from a user input, generated client-side, or whatever else, it is often the case that we&#8217;ll need to use it in more than one place to create the types of rich experiences users have come to expect from mobile. At the same time, resources like data usage and battery life are limited on mobile, so we want to keep things like network requests and computation to a minimum whenever possible. Keep it DRY (don&#8217;t repeat yourself), as the old saying goes.<\/p>\n<p>In today&#8217;s blog post, we&#8217;ll take a look at two ways to do this in an Ionic 2 app: Angular services and Ionic nav params.<br \/>\n<!--more--><\/p>\n<h3>Using an Angular Service<\/h3>\n<p>Let&#8217;s start with the slightly more complicated but more robust option of building an Angular service. Services allow us to create a shared instance of a class that we can inject into any component of our app via the Angular 2 dependency injection system.<\/p>\n<p>Here&#8217;s a quick example involving ice cream, since sharing ice cream is awesome.<\/p>\n<p>First, the skeleton of a service. All we need is to import Angular&#8217;s <code>Injectable<\/code> and a basic class declaration:<\/p>\n<pre><code>import { Injectable } from &#039;@angular\/core&#039;;\n\n@Injectable()\nexport class IceCreamService {\n\n  constructor() {}\n\n}\n<\/code><\/pre>\n<p>If you want to generate this boilerplate automatically, try using the <code>generate<\/code> command in the Ionic CLI, which will create the above plus a little extra for you in <code>src\/providers\/icecreamservice.ts<\/code>:<\/p>\n<p><code>ionic generate provider icecreamservice<\/code><\/p>\n<p>Syntactically, the only thing that makes this different from an Angular component is the use of the <code>@Injectable()<\/code> rather than the <code>@Component<\/code> decorator above the <code>export<\/code> statement. This tells the Angular compiler that this class is available for use in Angular&#8217;s dependency injection system.<\/p>\n<p>Next, let&#8217;s add some logic to the service:<\/p>\n<pre><code>export class IceCreamService {\n\n  hadIceCream: boolean = false;\n\n  constructor() {}\n\n  getIceCream() {\n    this.hadIceCream = true;\n    return &#039;mmmm... ice cream&#039;;    \n  }\n\n}\n<\/code><\/pre>\n<p>Pretty simple. We have a member variable that tells us if the user has had ice cream already, and a <code>getIceCream()<\/code> function. Next, let&#8217;s use it in a component by doing three things:<\/p>\n<ul>\n<li>importing the service<\/li>\n<li>adding the provider for the service in the component declaration<\/li>\n<li>creating an instance in the component&#8217;s constructor<\/li>\n<\/ul>\n<pre><code>import { Component } from &#039;@angular\/core&#039;;\nimport { NavController } from &#039;ionic-angular&#039;;\n\n\/\/ import the class from the service\nimport { IceCreamService } from &#039;..\/..\/providers\/icecreamservice&#039;;\n\n\/\/ the class name of the service is the provider\n@Component({\n  templateUrl: &#039;build\/pages\/home\/home.html&#039;,\n  providers: [IceCreamService]\n})\nexport class IceCreamPage {\n  \/\/ create an instance of the service\n  constructor(public iceCreamService: IceCreamService, public navCtrl: NavController) {\n\n  }\n}\n<\/code><\/pre>\n<p>Now, all we need to do is use the Valuable And Important data the service provides to our component about whether or not the user has already gotten their fair (and very generous, I assure you) allotment of ice cream:<\/p>\n<pre><code>export class IceCreamPage {\n  message: string = &quot;Ice cream. It&#039;s Good and You Want It.&quot;;\n  constructor(public iceCreamService: IceCreamService, public navCtrl: NavController) {}\n\n  getIceCream() {\n    if (!this.iceCreamService.hadIceCream) {\n      this.message = this.iceCreamService.getIceCream();\n    } else {\n      this.message = &#039;Stop hogging all the ice cream =(&#039;;\n    }\n  }\n}\n<\/code><\/pre>\n<p>Notice how the component can access any public member of the service. In this case, we use the <code>hadIceCream<\/code> variable in the conditional, as well as the <code>getIceCream()<\/code> function, and since we injected the service at the component level, it will share a single provider instance with all it&#8217;s child components. That&#8217;s pretty neat, but better yet, let&#8217;s change it so any component in our Ionic app will also have full and consistent access to a single instance of the provider.<\/p>\n<p>To do this, we keep the import statement for the <code>IceCreamService<\/code>, but remove <code>providers: [IceCreamService]<\/code> from the component definition in our <code>IceCreamPage<\/code>. Then we import the service and add the provider to the core <code>AppModule<\/code> of our app in <code>src\/app\/app.module.ts<\/code>:<\/p>\n<pre><code>...\nimport { IceCreamService } from &#039;..\/providers\/icecreamservice&#039;;\n\n@NgModule({\n\n...\n\n  providers: [IceCreamService]\n})\nexport class AppModule {}\n<\/code><\/pre>\n<h3>Using Ionic Nav Params<\/h3>\n<p>Our service is neat, but I&#8217;ll admit it could be a little bit of overkill for something as simple as ice cream. On to nav params!<\/p>\n<p>Nav params is a feature of navigation in Ionic that allows us to pass a JSON object from one page to the next. Using our ice cream app, let&#8217;s imagine for a moment that we are going to navigate to our <code>IceCreamPage<\/code> from a different view by using:<\/p>\n<p><code>this.navCtrl.push(IceCreamPage)<\/code><\/p>\n<p>All we need to do to use nav params is pass a JSON object as the second argument of <code>push()<\/code>:<\/p>\n<p><code>this.navCtrl.push(IceCreamPage, {status: true})<\/code><\/p>\n<p>To get this in our <code>IceCreamPage<\/code>, we import Ionic&#8217;s <code>NavParams<\/code> component:<\/p>\n<p><code>import { NavController, NavParams } from &#039;ionic-angular&#039;;<\/code><\/p>\n<p>Then create an instance of it in our constructor and call <code>get(key)<\/code> to retrieve the value from the nav params object. In this case, we call <code>get(&#039;status&#039;)<\/code>:<\/p>\n<pre><code>constructor(public navCtrl: NavController, public navParams: NavParams) { \n  if (!this.navParams.get(&#039;status&#039;)) {\n    this.message = &#039;Have some ice cream =)&#039;;\n  } else {\n    this.message = &#039;Stop hogging all the ice cream =(&#039;;\n  }\n}\n<\/code><\/pre>\n<h3>Decisions, Decisions<\/h3>\n<p>Mobile app development, like life, is full of choices. Services and nav params are both good ways of moving data around our app, but how do we pick? In a real-world scenario, you are almost definitely going to use both options in your app, but on a case-by-case basis in your application logic, the first thing to think about might be where the data is going. If we are navigating from one view to the next with either Ionic&#8217;s <code>NavController.push()<\/code> or <code>NavController.pop()<\/code>, then nav params could be a good choice. It&#8217;s lightweight and easy to implement. Conceptually, it also makes it very easy to reason about how data is flowing between views in our app, since we are passing it off directly.<\/p>\n<p>The next thing to consider is persistence. Will the data be needed in the future, or does it need to be used in more than one view of our app? If this is the case, a service is probably more appropriate. Since services create shared instances that can be injected into any component, they can ensure immutability and\/or consistency. In short, we can easily ensure that the data will be the same everywhere it is used, since every component will be consuming the same data source.<\/p>\n<p>But let&#8217;s get our priorities straight. The important thing is that the ice cream is safe and accounted for.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s pretty much unavoidable that we&#8217;re going to need to pass data around our app at some point. Why? Because apps are made up of a whole bunch of views and user interactions and&#8230;stuff, none of which tend to exist in isolation. Whether we&#8217;re talking about data pulled in via a REST API, collected from [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"publish_to_discourse":"","publish_post_category":"","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"","discourse_permalink":"","wpdc_publishing_response":"","wpdc_publishing_error":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[2,3,13,25],"class_list":["post-1445","post","type-post","status-publish","format-standard","hentry","category-all","tag-angularjs","tag-ionic","tag-ionic-2","tag-tutorials"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v23.0 (Yoast SEO v23.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Working with Data: Services and Nav Params - Ionic Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working with Data: Services and Nav Params\" \/>\n<meta property=\"og:description\" content=\"It&#8217;s pretty much unavoidable that we&#8217;re going to need to pass data around our app at some point. Why? Because apps are made up of a whole bunch of views and user interactions and&#8230;stuff, none of which tend to exist in isolation. Whether we&#8217;re talking about data pulled in via a REST API, collected from [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\" \/>\n<meta property=\"og:site_name\" content=\"Ionic Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-05T15:27:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-01-09T06:58:14+00:00\" \/>\n<meta name=\"author\" content=\"Alex Muramoto\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@alexmuramoto\" \/>\n<meta name=\"twitter:site\" content=\"@ionicframework\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alex Muramoto\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#article\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\"},\"author\":{\"name\":\"Alex Muramoto\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/c5087ed0d4175ef63cbc3aca6c41bcc7\"},\"headline\":\"Working with Data: Services and Nav Params\",\"datePublished\":\"2016-10-05T15:27:42+00:00\",\"dateModified\":\"2017-01-09T06:58:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\"},\"wordCount\":908,\"commentCount\":24,\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"keywords\":[\"angularjs\",\"Ionic\",\"Ionic 2\",\"Tutorials\"],\"articleSection\":[\"All\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\",\"url\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\",\"name\":\"Working with Data: Services and Nav Params - Ionic Blog\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/#website\"},\"datePublished\":\"2016-10-05T15:27:42+00:00\",\"dateModified\":\"2017-01-09T06:58:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ionic.io\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Working with Data: Services and Nav Params\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ionic.io\/blog\/#website\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"name\":\"ionic.io\/blog\",\"description\":\"Build amazing native and progressive web apps with the web\",\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ionic.io\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/ionic.io\/blog\/#organization\",\"name\":\"Ionic\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"width\":1920,\"height\":854,\"caption\":\"Ionic\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/ionicframework\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/c5087ed0d4175ef63cbc3aca6c41bcc7\",\"name\":\"Alex Muramoto\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2016\/08\/alex-lg-150x150.jpg\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2016\/08\/alex-lg-150x150.jpg\",\"caption\":\"Alex Muramoto\"},\"sameAs\":[\"https:\/\/x.com\/alexmuramoto\"],\"url\":\"https:\/\/ionic.io\/blog\/author\/alex\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Working with Data: Services and Nav Params - Ionic Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params","og_locale":"en_US","og_type":"article","og_title":"Working with Data: Services and Nav Params","og_description":"It&#8217;s pretty much unavoidable that we&#8217;re going to need to pass data around our app at some point. Why? Because apps are made up of a whole bunch of views and user interactions and&#8230;stuff, none of which tend to exist in isolation. Whether we&#8217;re talking about data pulled in via a REST API, collected from [&hellip;]","og_url":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params","og_site_name":"Ionic Blog","article_published_time":"2016-10-05T15:27:42+00:00","article_modified_time":"2017-01-09T06:58:14+00:00","author":"Alex Muramoto","twitter_card":"summary_large_image","twitter_creator":"@alexmuramoto","twitter_site":"@ionicframework","twitter_misc":{"Written by":"Alex Muramoto","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#article","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params"},"author":{"name":"Alex Muramoto","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/c5087ed0d4175ef63cbc3aca6c41bcc7"},"headline":"Working with Data: Services and Nav Params","datePublished":"2016-10-05T15:27:42+00:00","dateModified":"2017-01-09T06:58:14+00:00","mainEntityOfPage":{"@id":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params"},"wordCount":908,"commentCount":24,"publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"keywords":["angularjs","Ionic","Ionic 2","Tutorials"],"articleSection":["All"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params","url":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params","name":"Working with Data: Services and Nav Params - Ionic Blog","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/#website"},"datePublished":"2016-10-05T15:27:42+00:00","dateModified":"2017-01-09T06:58:14+00:00","breadcrumb":{"@id":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/ionic.io\/blog\/working-with-data-services-vs-nav-params#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ionic.io\/blog"},{"@type":"ListItem","position":2,"name":"Working with Data: Services and Nav Params"}]},{"@type":"WebSite","@id":"https:\/\/ionic.io\/blog\/#website","url":"https:\/\/ionic.io\/blog\/","name":"ionic.io\/blog","description":"Build amazing native and progressive web apps with the web","publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ionic.io\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ionic.io\/blog\/#organization","name":"Ionic","url":"https:\/\/ionic.io\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","width":1920,"height":854,"caption":"Ionic"},"image":{"@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/ionicframework"]},{"@type":"Person","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/c5087ed0d4175ef63cbc3aca6c41bcc7","name":"Alex Muramoto","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2016\/08\/alex-lg-150x150.jpg","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2016\/08\/alex-lg-150x150.jpg","caption":"Alex Muramoto"},"sameAs":["https:\/\/x.com\/alexmuramoto"],"url":"https:\/\/ionic.io\/blog\/author\/alex"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/1445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/comments?post=1445"}],"version-history":[{"count":0,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/1445\/revisions"}],"wp:attachment":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/media?parent=1445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/categories?post=1445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/tags?post=1445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}