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

Commit 5981bd3

Browse files
committed
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.
1 parent eddd7b8 commit 5981bd3

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

app/app.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
11
'use strict';
22

33
// Define the `phonecatApp` module
4-
var phonecatApp = angular.module('phonecatApp', []);
5-
6-
// Define the `PhoneListController` controller on the `phonecatApp` module
7-
phonecatApp.controller('PhoneListController', function PhoneListController($scope) {
8-
$scope.phones = [
9-
{
10-
name: 'Nexus S',
11-
snippet: 'Fast just got faster with Nexus S.'
12-
}, {
13-
name: 'Motorola XOOM™ with Wi-Fi',
14-
snippet: 'The Next, Next Generation tablet.'
15-
}, {
16-
name: 'MOTOROLA XOOM™',
17-
snippet: 'The Next, Next Generation tablet.'
18-
}
19-
];
20-
});
4+
angular.module('phonecatApp', []);

app/app.spec.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

app/index.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
<link rel="stylesheet" href="app.css" />
88
<script src="bower_components/angular/angular.js"></script>
99
<script src="app.js"></script>
10+
<script src="phone-list.component.js"></script>
1011
</head>
11-
<body ng-controller="PhoneListController">
12+
<body>
1213

13-
<ul>
14-
<li ng-repeat="phone in phones">
15-
<span>{{phone.name}}</span>
16-
<p>{{phone.snippet}}</p>
17-
</li>
18-
</ul>
14+
<!-- Use a custom component to render a list of phones -->
15+
<phone-list></phone-list>
1916

2017
</body>
2118
</html>

app/phone-list.component.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
// Register `phoneList` component, along with its associated controller and template
4+
angular.
5+
module('phonecatApp').
6+
component('phoneList', {
7+
template:
8+
'<ul>' +
9+
'<li ng-repeat="phone in $ctrl.phones">' +
10+
'<span>{{phone.name}}</span>' +
11+
'<p>{{phone.snippet}}</p>' +
12+
'</li>' +
13+
'</ul>',
14+
controller: function PhoneListController() {
15+
this.phones = [
16+
{
17+
name: 'Nexus S',
18+
snippet: 'Fast just got faster with Nexus S.'
19+
}, {
20+
name: 'Motorola XOOM™ with Wi-Fi',
21+
snippet: 'The Next, Next Generation tablet.'
22+
}, {
23+
name: 'MOTOROLA XOOM™',
24+
snippet: 'The Next, Next Generation tablet.'
25+
}
26+
];
27+
}
28+
});

app/phone-list.component.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
describe('phoneList', function() {
4+
5+
// Load the module that contains the `phoneList` component before each test
6+
beforeEach(module('phonecatApp'));
7+
8+
// Test the controller
9+
describe('PhoneListController', function() {
10+
11+
it('should create a `phones` model with 3 phones', inject(function($componentController) {
12+
var ctrl = $componentController('phoneList');
13+
14+
expect(ctrl.phones.length).toBe(3);
15+
}));
16+
17+
});
18+
19+
});

0 commit comments

Comments
 (0)