Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 724cff7

Browse files
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 Closes #207
1 parent 647e65a commit 724cff7

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

app/phone-list/phone-list.component.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,12 @@ angular.
55
module('phoneList').
66
component('phoneList', {
77
templateUrl: 'phone-list/phone-list.template.html',
8-
controller: function PhoneListController() {
9-
this.phones = [
10-
{
11-
name: 'Nexus S',
12-
snippet: 'Fast just got faster with Nexus S.',
13-
age: 1
14-
}, {
15-
name: 'Motorola XOOM™ with Wi-Fi',
16-
snippet: 'The Next, Next Generation tablet.',
17-
age: 2
18-
}, {
19-
name: 'MOTOROLA XOOM™',
20-
snippet: 'The Next, Next Generation tablet.',
21-
age: 3
22-
}
23-
];
8+
controller: function PhoneListController($http) {
9+
var self = this;
10+
self.orderProp = 'age';
2411

25-
this.orderProp = 'age';
12+
$http.get('phones/phones.json').then(function(response) {
13+
self.phones = response.data;
14+
});
2615
}
2716
});

app/phone-list/phone-list.component.spec.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,27 @@ describe('phoneList', function() {
77

88
// Test the controller
99
describe('PhoneListController', function() {
10-
var ctrl;
10+
var $httpBackend, ctrl;
11+
12+
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
13+
// This allows us to inject a service and assign it to a variable with the same name
14+
// as the service while avoiding a name conflict.
15+
beforeEach(inject(function($componentController, _$httpBackend_) {
16+
$httpBackend = _$httpBackend_;
17+
$httpBackend.expectGET('phones/phones.json')
18+
.respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
1119

12-
beforeEach(inject(function($componentController) {
1320
ctrl = $componentController('phoneList');
1421
}));
1522

16-
it('should create a `phones` model with 3 phones', function() {
17-
expect(ctrl.phones.length).toBe(3);
23+
it('should create a `phones` property with 2 phones fetched with `$http`', function() {
24+
expect(ctrl.phones).toBeUndefined();
25+
26+
$httpBackend.flush();
27+
expect(ctrl.phones).toEqual([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
1828
});
1929

20-
it('should set a default value for the `orderProp` model', function() {
30+
it('should set a default value for the `orderProp` property', function() {
2131
expect(ctrl.orderProp).toBe('age');
2232
});
2333

e2e-tests/scenarios.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ describe('PhoneCat Application', function() {
1515
var phoneList = element.all(by.repeater('phone in $ctrl.phones'));
1616
var query = element(by.model('$ctrl.query'));
1717

18-
expect(phoneList.count()).toBe(3);
18+
expect(phoneList.count()).toBe(20);
1919

2020
query.sendKeys('nexus');
2121
expect(phoneList.count()).toBe(1);
2222

2323
query.clear();
2424
query.sendKeys('motorola');
25-
expect(phoneList.count()).toBe(2);
25+
expect(phoneList.count()).toBe(8);
2626
});
2727

2828
it('should be possible to control phone order via the drop-down menu', function() {

0 commit comments

Comments
 (0)