-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavatar.js
More file actions
59 lines (52 loc) · 1.61 KB
/
avatar.js
File metadata and controls
59 lines (52 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 1. We have added a directive with the name 'avatar' and handler of
* avatarDirective to our angular app module
*/
angular.module('app', [])
.controller('mainCtrl', mainCtrl)
.directive('avatar', avatarDirective);
function mainCtrl ($scope) {
$scope.users = [];
$scope.addNew = function (user) {
$scope.users.push({
name: user.name,
avatarUrl: user.url,
email: user.email
}); /* [1] */
user.name = ''; /* [2] */
user.url = '';
user.email = '';
};
}
/**
* 1. this defines the api of our avatar directive. This means we are
* expecting a user property whose value should be interpreted as an object.
* 2. This simply means we want this directive to be used as an element.
* 3. You can see here we've moved the html that was in our template before
* and give it as the template for this directive. This means wherever we use
* <avatar /> this html will also be placed there.
* 4. Here we are implementing the feature where if there is no user avatar url,
* we go ahead and give it a default
*/
function avatarDirective () {
return {
scope: {
user: '=' /* [1] */
},
restrict: 'E', /* [2] */
replace: 'true',
template: (
'<div class="Avatar">' +
'<img ng-src="{{user.avatarUrl}}" />' +
'<h4>{{user.name}}</h4>' +
'<h4>{{user.email}}</h4>' +
'</div>'
), /* [3] */
link: link
};
function link (scope) { /* [4] */
if (!scope.user.avatarUrl) {
scope.user.avatarUrl = 'https://www.drupal.org/files/issues/default-avatar.png';
}
}
}