33angular . module ( 'angularify.semantic.dropdown' , [ ] )
44
55. controller ( 'DropDownController' , [ '$scope' , function ( $scope ) {
6+ $scope . items = [ ] ;
7+
8+ this . add_item = function ( scope ) {
9+ $scope . items . push ( scope ) ;
10+
11+ scope . $on ( '$destroy' , function ( event ) {
12+ this . remove_accordion ( scope ) ;
13+ } ) ;
14+
15+ return $scope . items ;
16+ }
17+
18+ this . remove_item = function ( scope ) {
19+ var index = $scope . items . indexOf ( scope ) ;
20+ if ( index !== - 1 )
21+ $scope . items . splice ( index , 1 ) ;
22+ }
23+
24+ this . update_title = function ( title ) {
25+ var i = 0 ;
26+ for ( i in $scope . items ) {
27+ $scope . items [ i ] . title = title ;
28+ }
29+ }
630
731} ] )
832
@@ -14,24 +38,48 @@ angular.module('angularify.semantic.dropdown', [])
1438 controller : 'DropDownController' ,
1539 scope : {
1640 title : "@" ,
17- open : "@"
41+ open : "@" ,
42+ model : '=ngModel'
1843 } ,
1944 template : "<div class=\"{{dropdown_class}}\">" +
2045 "<div class=\"default text\">{{title}}</div>" +
2146 "<i class=\"dropdown icon\"></i>" +
2247 "<div class=\"menu\" ng-transclude>" +
2348 "</div>" +
2449 "</div>" ,
25- link : function ( scope , element , attrs ) {
50+ link : function ( scope , element , attrs , DropDownController ) {
2651 scope . dropdown_class = 'ui selection dropdown' ;
27-
52+
2853 if ( scope . open == "true" ) {
2954 scope . open = true ;
3055 scope . dropdown_class = scope . dropdown_class + ' active visible' ;
3156 }
3257 else
3358 scope . open = false ;
3459
60+ DropDownController . add_item ( scope ) ;
61+
62+ //
63+ // Watch for title changing
64+ //
65+ scope . $watch ( 'title' , function ( val ) {
66+ if ( val == undefined )
67+ return ;
68+
69+ if ( val == scope . title )
70+ return ;
71+
72+ scope . model = val ;
73+ } ) ;
74+
75+ //
76+ // Watch for ng-model changing
77+ //
78+ scope . $watch ( 'model' , function ( val ) {
79+ // update title
80+ DropDownController . update_title ( val )
81+ } ) ;
82+
3583 //
3684 // Click handler
3785 //
@@ -54,9 +102,17 @@ angular.module('angularify.semantic.dropdown', [])
54102 replace : true ,
55103 transclude : true ,
56104 require : '^dropdown' ,
57- template : '<div class="item" ng-transclude></div>' ,
58- link : function ( scope , element , attrs ) {
59-
105+ scope : {
106+ } ,
107+ template : '<div class="item" ng-transclude>{{title}}</div>' ,
108+ link : function ( scope , element , attrs , DropDownController ) {
109+ var title = element . children ( ) [ 0 ] . innerHTML ;
110+ //
111+ // Menu item click handler
112+ //
113+ element . bind ( 'click' , function ( ) {
114+ DropDownController . update_title ( title )
115+ } ) ;
60116 }
61117 }
62118} ) ;
0 commit comments