2018-07-25AngularJS Phone Catalog Tutorial Application

AngularJS Phone Catalog Tutorial Application

Overview

This application takes the developer through the process of building a web-application using
AngularJS. The application is loosely based on the Google Phone Gallery, which no longer exists.
Here is a historical reference: Google Phone Gallery on WayBack

Each tagged commit is a separate lesson teaching a single aspect of the framework.

The full tutorial can be found at https://docs.angularjs.org/tutorial.

Prerequisites

Git

  • A good place to learn about setting up git is here.
  • You can find documentation and download git here.

Node.js and Tools

  • Get Node.js.
  • Install the tool dependencies: npm install

Workings of the Application

  • The application filesystem layout structure is based on the angular-seed project.
  • There is no dynamic backend (no application server) for this application. Instead we fake the
    application server by fetching static JSON files.
  • Read the Development section at the end to familiarize yourself with running and developing
    an Angular application.

Commits / Tutorial Outline

You can check out any point of the tutorial using:

git checkout step-?

To see the changes made between any two lessons use the git diff command:

git diff step-?..step-?

step-0 Bootstrapping

  • Add the 'angular.js' script.
  • Add the ngApp directive to bootstrap the application.
  • Add a simple template with an expression.

step-1 Static Template

  • Add a stylesheet file ('app/app.css').
  • Add a static list with two phones.

step-2 Angular Templates

  • Convert the static phone list to dynamic by:
    • Creating a PhoneListController controller.
    • Extracting the data from HTML into the controller as an in-memory dataset.
    • Converting the static document into a template with the use of the ngRepeat directive.
  • Add a simple unit test for the PhoneListController controller to show how to write tests and
    run them using Karma.

step-3 Components

  • Introduce components.
  • Combine the controller and the template into a reusable, isolated phoneList component.
  • Refactor the application and tests to use the phoneList component.

step-4 Directory and File Organization

  • Refactor the layout of files and directories, applying best practices and techniques that will
    make the application easier to maintain and expand in the future:
    • Put each entity in its own file.
    • Organize code by feature area (instead of by function).
    • Split code into modules that other modules can depend on.
    • Use external templates in .html files (instead of inline HTML strings).

step-5 Filtering Repeaters

  • Add a search box to demonstrate:
    • How the data-binding works on input fields.
    • How to use the filter filter.
    • How ngRepeat automatically shrinks and grows the number of phones in the view.
  • Add an end-to-end test to:
    • Show how end-to-end tests are written and used.
    • Prove that the search box and the repeater are correctly wired together.

step-6 Two-way Data Binding

  • Add an age property to the phone model.
  • Add a drop-down menu to control the phone list order.
  • Override the default order value in controller.
  • Add unit and end-to-end tests for this feature.

step-7 XHR & Dependency Injection

  • Replace the in-memory dataset with data loaded from the server (in the form of a static
    'phone.json' file to keep the tutorial backend agnostic):
    • The JSON data is loaded using the $http service.
  • Demonstrate the use of services and dependency injection (DI):
    • $http is injected into the controller through DI.
    • Introduce DI annotation methods: .$inject and inline array

step-8 Templating Links & Images

  • Add a phone image and links to phone pages.
  • Add an end-to-end test that verifies the phone links.
  • Tweak the CSS to style the page just a notch.

step-9 Routing & Multiple Views

  • Introduce the $route service, which allows binding URLs to views for routing and deep-linking:
    • Add the ngRoute module as a dependency.
    • Configure routes for the application.
    • Use the ngView directive in 'index.html'.
  • Create a phone list route (/phones):
    • Map /phones to the existing phoneList component.
  • Create a phone detail route (/phones/:phoneId):
    • Map /phones/:phoneId to a new phoneDetail component.
    • Create a dummy phoneDetail component, which displays the selected phone ID.
    • Pass the phoneId parameter to the component's controller via $routeParams.

step-10 More Templating

  • Implement fetching data for the selected phone and rendering to the view:
    • Use $http in PhoneDetailController to fetch the phone details from a JSON file.
    • Create the template for the detail view.
  • Add CSS styles to make the phone detail page look "pretty-ish".

step-11 Custom Filters

  • Implement a custom checkmark filter.
  • Update the phoneDetail template to use the checkmark filter.
  • Add a unit test for the checkmark filter.

step-12 Event Handlers

  • Make the thumbnail images in the phone detail view clickable:
    • Introduce a mainImageUrl property on PhoneDetailController.
    • Implement the setImage() method for changing the main image.
    • Use ngClick on the thumbnails to register a handler that changes the main image.
    • Add an end-to-end test for this feature.

step-13 REST and Custom Services

  • Replace $http with $resource.
  • Create a custom Phone service that represents the RESTful client.
  • Use a custom Jasmine equality tester in unit tests to ignore irrelevant properties.

step-14 Animations

  • Add animations to the application:
    • Animate changes to the phone list, adding, removing and reordering phones with ngRepeat.
    • Animate view transitions with ngView.
    • Animate changes to the main phone image in the phone detail view.
  • Showcase three different kinds of animations:
    • CSS transition animations.
    • CSS keyframe animations.
    • JavaScript-based animations.

Development with angular-phonecat

The following docs describe how you can test and develop this application further.

Installing Dependencies

The application relies upon various Node.js tools, such as Bower, Karma and
Protractor. You can install these by running:

npm install

This will also run Bower, which will download the Angular files needed for the current step of the
tutorial.

Most of the scripts described below will run this automatically but it doesn't do any harm to run
it whenever you like.

Running the Application during Development

Unit Testing

We recommend using Jasmine and Karma for your unit tests/specs, but you are free
to use whatever works for you.

  • Start Karma with npm test.
  • A browser will start and connect to the Karma server. Chrome and Firefox are the default browsers,
    others can be captured by loading the same URL or by changing the karma.conf.js file.
  • Karma will sit and watch your application and test JavaScript files. To run or re-run tests just
    change any of your these files.

End-to-End Testing

We recommend using Protractor for end-to-end (e2e) testing.

It requires a webserver that serves the application. See the
Running the Application during Development section, above.

  • Serve the application with: npm start
  • In a separate terminal/command line window run the e2e tests: npm run protractor.
  • Protractor will execute the e2e test scripts against the web application itself. The project is
    set up to run the tests on Chrome directly. If you want to run against other browsers, you must
    modify the configuration at e2e-tests/protractor-conf.js.

Note:
Under the hood, Protractor uses the Selenium Standalone Server, which in turn requires
the Java Development Kit (JDK) to be installed on your local machine. Check this by running
java -version from the command line.

If JDK is not already installed, you can download it here.

Application Directory Layout

app/                     --> all the source code of the app (along with unit tests)
  bower_components/...   --> 3rd party JS/CSS libraries, including Angular and jQuery
  core/                  --> all the source code of the core module (stuff used throughout the app)
    checkmark/...        --> files for the `checkmark` filter, including JS source code, specs
    phone/...            --> files for the `core.phone` submodule, including JS source code, specs
    core.module.js       --> the core module
  img/...                --> image files
  phone-detail/...       --> files for the `phoneDetail` module, including JS source code, HTML templates, specs
  phone-list/...         --> files for the `phoneList` module, including JS source code, HTML templates, specs
  phones/...             --> static JSON files with phone data (used to fake a backend API)
  app.animations.css     --> hooks for running CSS animations with `ngAnimate`
  app.animations.js      --> hooks for running JS animations with `ngAnimate`
  app.config.js          --> app-wide configuration of Angular services
  app.css                --> default stylesheet
  app.module.js          --> the main app module
  index.html             --> app layout file (the main HTML template file of the app)

e2e-tests/               --> config and source files for e2e tests
  protractor.conf.js     --> config file for running e2e tests with Protractor
  scenarios.js           --> e2e specs

node_modules/...         --> development tools (fetched using `npm`)

scripts/                 --> handy scripts
  private/...            --> private scripts used by the Angular Team to maintain this repo
  update-repo.sh         --> script for pulling down the latest version of this repo (!!! DELETES ALL CHANGES YOU HAVE MADE !!!)

bower.json               --> Bower specific metadata, including client-side dependencies
karma.conf.js            --> config file for running unit tests with Karma
package.json             --> Node.js specific metadata, including development tools dependencies

Contact

For more information on AngularJS, please check out https://angularjs.org/.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,039评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,426评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,417评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,868评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,892评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,692评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,416评论 3 419
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,326评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,782评论 1 316
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,957评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,102评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,790评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,442评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,996评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,113评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,332评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,044评论 2 355

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,334评论 0 10
  • 一个雨夜,飘着毛毛细雨,走出化妆室的门口,风刮着脸庞,刺骨而冰冷,骑着自己的小绵羊走在空旷的路上。耳边响着音乐,那...
    素维维维阅读 435评论 0 0
  • 好吧,一开始我就打错了字,其实这家店名叫回味湘 店名的来由,我想是因为老板是湖南人,具体哪个市的我问过,实在没记住...
    itlaborer阅读 344评论 0 1
  • 最近,因为家里的事,心情一直处于抑郁状态,对什么事情都提不起兴趣,看着同学们打卡,忙的不亦乐乎,一直鼓励自...
    蓝幽_e733阅读 420评论 0 0