Skip to content

Commit

Permalink
step-4 components
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Sep 12, 2012
1 parent da1ceb9 commit b541fb5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 deletions.
32 changes: 32 additions & 0 deletions app/css/app.css
Expand Up @@ -5,3 +5,35 @@ h1, h2, h3, h4 {
body {
padding: .5em 1em;
}

.header {
text-align: right;
}

div[ng-repeat] {
display: inline-block;
}

.profile > img {
width: 80px;
height: 80px;
}

.profile {
margin: 3px;
width: 120px;
height: 120px;
display: inline-block;
border: 1px solid black;
-webkit-border-radius: 3px;
padding: 5px;
text-align: center;
overflow: hidden;
background-color: #EEE;
}

.profile>h1 {
margin: 0;
padding: 0;
font-size: 11px;
}
40 changes: 24 additions & 16 deletions app/index.html
Expand Up @@ -15,15 +15,25 @@
<script src="js/bootstrap.js"></script>
</head>
<body ng-controller="Demo">
Name: <input ng-model="name">
<hr>
<div class="header">
<h1>I am</h1>
<div class="profile">
<img ng-src="{{email|gravatar}}">
<h1>{{email}}</h1>
</div>
</div>

<hr/>

<ul>
<li ng-repeat="i in [1, 2, 3]">
<div demo-greet="name"></div>
<tt>leak={{leak}}</tt>
</li>
</ul>
<div>
<h1>AngularJS Developers</h1>
<div ng-repeat="email in devs">
<div class="profile">
<img ng-src="{{email|gravatar}}">
<h1>{{email}}</h1>
</div>
</div>
</div>

<hr>
<pre>name={{name}}
Expand All @@ -32,12 +42,10 @@
</html>

<!--
- Show the controller
- Show difference between compile and link
- Explain restrict / Directive invocation
- Show how directors can leak state
- Show how scope encapsulates state
- Show how to watch an expression
- Show how to change the state
- Show Dependency Injection
- Show HTML duplication
- Show extraction to separate file
- Explain 'restrict'
- Show leaking into component
- Show leaking out of component
- Show component parameters
-->
8 changes: 8 additions & 0 deletions app/js/controllers.js
Expand Up @@ -3,4 +3,12 @@
myApp.controller('Demo', function($scope) {
$scope.name = 'world';
$scope.leak = 'none';

$scope.email = 'misko@hevery.com';

$scope.devs = [
'misko@hevery.com',
'iiminar@gmail.com',
'vojta.jina@gmail.com'
];
});
24 changes: 3 additions & 21 deletions app/js/directives.js
@@ -1,26 +1,8 @@
'use strict';

myApp.directive('demoGreet', function($parse) {
myApp.directive('profile', function() {
return {
scope: true,
compile: function compileFn(cElement, attrs) {
console.log('compileFn(', cElement, attrs, ')');
cElement.addClass('greet');
return function linkFn(scope, lElement, attrs) {
console.log('linkingFn(', scope, lElement, attrs, ')', cElement == lElement);

scope.leak = 'LEAKING';

scope.$watch(attrs.greet, function(name) {
lElement.text('Hello ' + name + '!');
});

lElement.bind('click', function() {
scope.$apply(function() {
$parse(attrs.greet).assign(scope, 'WORLD');
});
});
}
}
//restrict: 'E',
//templateUrl: 'partials/profile.html'
};
});
7 changes: 7 additions & 0 deletions app/js/filters.js
@@ -1,2 +1,9 @@
'use strict';

myApp.filter('gravatar', function() {
return function(email) {
if (email) {
return 'http://www.gravatar.com/avatar/' + hex_md5(email) + '.jpg';
}
}
});
4 changes: 4 additions & 0 deletions app/partials/profile.html
@@ -0,0 +1,4 @@
<div class="profile">
<img ng-src="{{email|gravatar}}">
<h1>{{email}}</h1>
</div>

0 comments on commit b541fb5

Please sign in to comment.