7
7
< body ng:init ="$window.$root = this ">
8
8
9
9
< script >
10
- function TicTacToeCntl ( ) {
11
- this . cellStyle = {
12
- 'height' : '20px' ,
13
- 'width' : '20px' ,
14
- 'border' : '1px solid black' ,
15
- 'text-align' : 'center' ,
16
- 'vertical-align' : 'middle' ,
17
- 'cursor' : 'pointer'
18
- } ;
19
- this . reset ( ) ;
20
- this . $watch ( '$location.hashPath' , this . readUrl ) ;
21
- }
22
- TicTacToeCntl . prototype = {
23
- dropPiece : function ( row , col ) {
24
- if ( ! this . winner && ! this . board [ row ] [ col ] ) {
25
- this . board [ row ] [ col ] = this . nextMove ;
26
- this . nextMove = this . nextMove == 'X' ? 'O' : 'X' ;
27
- this . grade ( ) ;
28
- this . setUrl ( ) ;
29
- }
30
- } ,
31
- reset : function ( ) {
32
- this . board = [
33
- [ '' , '' , '' ] ,
34
- [ '' , '' , '' ] ,
35
- [ '' , '' , '' ]
36
- ] ;
37
- this . nextMove = 'X' ;
38
- this . winner = '' ;
39
- this . setUrl ( ) ;
40
- } ,
41
- grade : function ( ) {
42
- var b = this . board ;
43
- this . winner =
44
- row ( 0 ) || row ( 1 ) || row ( 2 ) ||
45
- col ( 0 ) || col ( 1 ) || col ( 2 ) ||
46
- diagonal ( - 1 ) || diagonal ( 1 ) ;
47
- function row ( r ) { return same ( b [ r ] [ 0 ] , b [ r ] [ 1 ] , b [ r ] [ 2 ] ) ; }
48
- function col ( c ) { return same ( b [ 0 ] [ c ] , b [ 1 ] [ c ] , b [ 2 ] [ c ] ) ; }
49
- function diagonal ( i ) { return same ( b [ 0 ] [ 1 - i ] , b [ 1 ] [ 1 ] , b [ 2 ] [ 1 + i ] ) ; }
50
- function same ( a , b , c ) { return ( a == b && b == c ) ? a : '' ; } ;
51
- } ,
52
- setUrl : function ( ) {
53
- var rows = [ ] ;
54
- angular . foreach ( this . board , function ( row ) {
55
- rows . push ( row . join ( ',' ) ) ;
10
+ angular . widget ( 'my:greeter' , function ( compileElement ) {
11
+ var compiler = this ;
12
+ compileElement . css ( 'style' , 'block' ) ;
13
+ var salutaitonExp = compileElement . attr ( 'salutation' ) ;
14
+ var nameExp = compileElement . attr ( 'name' ) ;
15
+ return function ( linkElement ) {
16
+ var salutaitonSpan = angular . element ( '<span class="salutation"></span' ) ;
17
+ var nameSpan = angular . element ( '<span class="name"></span>' ) ;
18
+ linkElement . append ( salutaitonSpan ) ;
19
+ linkElement . append ( compiler . text ( ' ' ) ) ;
20
+ linkElement . append ( nameSpan ) ;
21
+ linkElement . append ( compiler . text ( '!' ) ) ;
22
+ this . $watch ( salutaitonExp , function ( value ) {
23
+ salutaitonSpan . text ( value ) ;
24
+ } ) ;
25
+ this . $watch ( nameExp , function ( value ) {
26
+ nameSpan . text ( value ) ;
56
27
} ) ;
57
- this . $location . hashPath = rows . join ( ';' ) + '/' + this . nextMove ;
58
- } ,
59
- readUrl : function ( value ) {
60
- if ( value ) {
61
- value = value . split ( '/' ) ;
62
- this . nextMove = value [ 1 ] ;
63
- angular . foreach ( value [ 0 ] . split ( ';' ) , function ( row , i ) {
64
- this . board [ i ] = row . split ( ',' ) ;
65
- } , this ) ;
66
- } else {
67
- this . reset ( ) ;
68
- }
69
- }
70
- } ;
28
+ } ;
29
+ } ) ;
71
30
</ script >
72
- < h3 > Tic-Tac-Toe</ h3 >
73
- Next Player: {{nextMove}}
74
- < div ng:show ="winner "> Player {{winner}} has won!</ div >
75
- < table ng:controller ="TicTacToeCntl ">
76
- < tr ng:repeat ="row in board " style ="height:15px; ">
77
- < td ng:repeat ="cell in row " ng:style ="cellStyle "
78
- ng:click ="dropPiece($parent.$index, $index) "> {{cell}}</ td >
79
- </ tr >
80
- </ table >
81
- < button ng:click ="reset() "> reset board</ button >
31
+ < div ng:init ="salutation='Hello'; name='World' ">
32
+ < my:greeter salutation ="salutation " name ="name "/>
33
+ </ div >
82
34
83
35
</ body >
84
36
</ html >
0 commit comments