Guest User

Untitled

a guest
Dec 15th, 2017
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. Let’s start with a simple requirement and then build our way up. We’ll be working on a component for the ‘LetsConnect’ app that lets you connect with other business people. We want to create a component to display the user’s business card. The component has the following requirements:
  2.  
  3. 1) Should show the person’s name, title, and contact email.
  4. 2) It should use the personal information being fetched from another service.
  5. Let’s start with the first requirement and work our way up:
  6. Here is the boiler plate for our AngularJS component
  7. The interface for a component is defined under IComponentOptions which we will need to import into our file. I like to keep the template separate, so we will use templateUrl to specify the file location. If you’d prefer, you can use template and define the template inline. In addition to the template we will be using a controller, and a few bindings so that our component can accept the name, title, and contact email.
  8.  
  9. In the constructor, we specify all the values we just defined. We will be using “@” for the binding the name, title, and contact so that it accepts strings.
  10. import { IComponentOptions } from 'angular';
  11.  
  12. export class businessCardComponent implements IComponentOptions {
  13. public templateUrl: string;
  14. public controller: string;
  15. public bindings: any;
  16.  
  17. constructor() {
  18. this.templateUrl = 'components/business-card/business-card.html';
  19. this.controller = 'BusinessCardController';
  20. this.bindings = {
  21. personName: '@',
  22. personTitle: '@',
  23. personContact: '@'
  24. };
  25. }
  26. }
  27.  
  28. angular.module('ConnectWithMe').component('businessCardComponent', new businessCardComponent());
  29.  
  30. Now let’s look into the controller that will be handling the logic for this component, the “BusinessCardController”.
  31.  
  32. interface IBusinessCardController {
  33. personName: string;
  34. personTitle: string;
  35. personContact: string;
  36. }
  37.  
  38. export class BusinessCardController implements IBusinessCardController {
  39.  
  40. public personName: string;
  41. public personTitle: string;
  42. public personContact: string;
  43.  
  44. }
  45.  
  46. angular.module('ConnectWithMe').component('BusinessCardController', BusinessCardController);
  47.  
  48. For controllers, it’s a good idea to define an interface. This is so that every controller that creates business cards will need a name, a title, and a contact.
  49.  
  50. Finally, we build the view. I’m going to skip the CSS for now. You can access the controller’s public properties in the $ctrl object. You can specify a more specific alias using the controllerAs when defining your component.
  51.  
  52. <div>
  53. <div>Name: {{::$ctrl.personName}}</div>
  54. <div>Type: {{::$ctrl.personTitle}}</div>
  55. <div>Contact: {{::$ctrl.personContact}}</div>
  56. </div>
  57.  
  58. We have all the three pieces required to get our component to work. We can use this component in two ways
  59. A) Inside a template by using it as an element like so:
  60. <div>
  61. <business-card-component person-name="John Doe" person-title="Software Engineer" person-contact="john@doe.com"></business-card-component>
  62. <div>Some other stuff in the website</div>
  63. </div>
  64.  
  65. B) Use the component in the UI-Router.
  66. I found this part to be a little confusing because the implementation is a little different based on the version of angular and ui-router you are on.
  67.  
  68. I am using angular 1.5.x and ui-router 0.3.2 and I use the following when defining my route:
  69.  
  70. {
  71. url: '/business-card',
  72. template: '<business-card-component person-name="John Doe" person-title="Software Engineer" person-contact="john@doe.com"></business-card-component>',
  73. };
  74.  
  75.  
  76. If you’re using ui-router > 1.xx you have access to the component keyword, and it makes using components easier.
  77.  
  78.  
  79. We have the basic component setup, now let’s tackle the second requirement: Use data from a service to populate the component.
  80.  
  81. There are a few ways to do this and the most common way is to inject the service into the controller, and use getters to fetch the data. However, I want to keep my component independent of what service returns the data. This way I can swap out different services, and make this component as a reusable piece of code.
  82.  
  83. First, we need to change the data bindings in our component definition from ‘@’ to ‘<’ (‘<’ is to enable one-way data binding)
  84.  
  85. Now our component will look like:
  86.  
  87. import { IComponentOptions } from 'angular';
  88.  
  89. export class BusinessCardComponent implements IComponentOptions {
  90. public templateUrl: string;
  91. public controller: string;
  92. public bindings: any;
  93.  
  94. constructor() {
  95. this.templateUrl = 'components/business-card/business-card.html';
  96. this.controller = 'BusinessCardController';
  97. this.bindings = {
  98. personName: '<',
  99. personTitle: '<',
  100. personContact: '<'
  101. };
  102. }
  103. }
  104.  
  105. angular.module('ConnectWithMe').component('BusinessCardComponent', new BusinessCardComponent());
  106.  
  107. Our controller can stay the same because we are not making any logical changes. The way we use our component, however, needs to change.
  108.  
  109. Let’s look at the two forms of implementation again. 1) Using the component as an element. 2) Using the component as a route.
  110.  
  111. 1) Using it as an Element
  112. Imagine that this component is being used within a template that has its own controller, let’s call it the parentController. The parentController has access to the personService, which pulls the user’s name, contact, and Title.
  113.  
  114. <div>
  115. <business-card-component person-name="parentController.getName()" person-title="parentController.getTitle()" person-contact="parentController.getName()"></business-card-component>
  116. <div>Some other stuff in the website</div>
  117. </div>
  118.  
  119. 2) Using the component as a route
  120. To pass data into our component, we’ll have to use the resolve block to pull the data from the service.
  121.  
  122. {
  123. url: '/business-card',
  124. template: '<business-card-component person-name="$resolve.name" person-title="$resolve.title" person-contact="$resolve.contact"></business-card-component>',
  125. resolve: {
  126. name: (DataService): string => {
  127. return DataService.getName();
  128. },
  129. title: (DataService): string => {
  130. return DataService.getTitle();
  131. },
  132. contact: (DataService): string => {
  133. return DataService.getEmail();
  134. }
  135. }
  136. }
  137.  
  138. Since we are using the legacy ui-router, we will use the template. If you’re using ui-router 1x you can use the component keyword.
Add Comment
Please, Sign In to add comment