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

Commit e02ad1e

Browse files
step-5 XHR and dependency injection
- Replaced the in-memory dataset with data loaded from the server (in the form of static phone.json file to make this tutorial backend agnostic) - The json file is loaded using the [$http] service - Demonstrate the use of [services][service] and [dependency injection][DI] - The [$http] is injected into the controller through [dependency injection][DI] (Added inline annotation - thanks to @guatebus - closes #207)
1 parent 863ec8e commit e02ad1e

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

app/js/controllers.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@
44

55
var phonecatApp = angular.module('phonecatApp', []);
66

7-
phonecatApp.controller('PhoneListCtrl', function($scope) {
8-
$scope.phones = [
9-
{'name': 'Nexus S',
10-
'snippet': 'Fast just got faster with Nexus S.',
11-
'age': 1},
12-
{'name': 'Motorola XOOM™ with Wi-Fi',
13-
'snippet': 'The Next, Next Generation tablet.',
14-
'age': 2},
15-
{'name': 'MOTOROLA XOOM™',
16-
'snippet': 'The Next, Next Generation tablet.',
17-
'age': 3}
18-
];
7+
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {
8+
$http.get('phones/phones.json').success(function(data) {
9+
$scope.phones = data;
10+
});
1911

2012
$scope.orderProp = 'age';
21-
});
13+
}]);

test/e2e/scenarios.js

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

19-
expect(phoneList.count()).toBe(3);
19+
expect(phoneList.count()).toBe(20);
2020

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

2424
query.clear();
2525
query.sendKeys('motorola');
26-
expect(phoneList.count()).toBe(2);
26+
expect(phoneList.count()).toBe(8);
2727
});
2828

2929

test/unit/controllersSpec.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
describe('PhoneCat controllers', function() {
55

66
describe('PhoneListCtrl', function(){
7-
var scope, ctrl;
7+
var scope, ctrl, $httpBackend;
88

99
beforeEach(module('phonecatApp'));
10+
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
11+
$httpBackend = _$httpBackend_;
12+
$httpBackend.expectGET('phones/phones.json').
13+
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
1014

11-
beforeEach(inject(function($controller) {
12-
scope = {};
13-
ctrl = $controller('PhoneListCtrl', {$scope:scope});
15+
scope = $rootScope.$new();
16+
ctrl = $controller('PhoneListCtrl', {$scope: scope});
1417
}));
1518

1619

17-
it('should create "phones" model with 3 phones', function() {
18-
expect(scope.phones.length).toBe(3);
20+
it('should create "phones" model with 2 phones fetched from xhr', function() {
21+
expect(scope.phones).toBeUndefined();
22+
$httpBackend.flush();
23+
24+
expect(scope.phones).toEqual([{name: 'Nexus S'},
25+
{name: 'Motorola DROID'}]);
1926
});
2027

2128

0 commit comments

Comments
 (0)