Skip to content

Instantly share code, notes, and snippets.

@tcdevs
Created August 13, 2013 14:17
Show Gist options
  • Select an option

  • Save tcdevs/6221538 to your computer and use it in GitHub Desktop.

Select an option

Save tcdevs/6221538 to your computer and use it in GitHub Desktop.
Angular JS - Using Directives to Create Custom Attributes - http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=464939
var dsApp = angular.module('dsApp', []);
2
3
4 dsApp.directive('ngDsFade', function () {
5 return function (scope, element, attrs) {
6 element.css('display', 'none');
7 scope.$watch(attrs.ngDsFade, function (value) {
8 if (value) {
9 element.fadeIn(200);
10 } else {
11 element.fadeOut(100);
12 }
13 });
14 }
15 });
16
17 dsApp.directive('ngDsActive', function () {
18 return function (scope, element, attrs) {
19 scope.$watch(attrs.ngDsActive, function (value) {
20 if (value) {
21 element.addClass('active');
22 } else {
23 element.removeClass('active');
24 }
25 });
26 }
27 });
28 dsApp.directive('ngDsInlineTemplate',function(){
29 return function(scope,element,attrs)
30 {
31 var templateElement = $(element);
32 var name= templateElement.attr("ng-ds-inline-template");
33 $(element).hide();//hide our template.
34 scope[name]=element;//save a pointer to the template node in the
35
36
37 }
38 });
39 dsApp.directive('ngDsInlineEdit',function()
40 {
41 return function(scope,element,attrs)
42 {
43 //$(element).hide();//hide our template.
44 scope.$watch(attrs.ngDsInlineEdit,function(value)
45 {
46 //we need to get our template element to move it and show, or just hide it.
47 if(!attrs.ngDsTemplateName)
48 return;//we don't have a template yet.
49 var template = $(scope[attrs.ngDsTemplateName]);
50 if(value)
51 {
52 setTimeout(function(){
53 //move the template here and show it.
54 $(element).after(template);
55 template.fadeIn(200);
56 },10);
57 }
58 else
59 {
60 template.hide();
61 }
62
63 });
64
65 }
66 });
67
68 dsApp.directive('ngDsModal', function () {
69 return function (scope, element, attrs) {
70 var diag = $(element).dialog(
71 {autoOpen:false,
72 close:function()
73 {
74 try
75 {
76 var functionName= attrs.ngDsModalClosed;
77 //alert(functionName);
78 if(typeof scope[functionName]=="function")
79 setTimeout(function(){scope.$apply(scope[functionName]);},100);
80 }
81 catch(err)
82 {}
83 }
84 });
85 scope.$watch(attrs.ngDsModal, function (value) {
86 if (value) {
87 $(element).dialog("open");
88 }
89 else
90 {
91 $(element).dialog("close");
92
93 }
94 });
95 }
96 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment