Last active
August 29, 2015 14:05
-
-
Save srfrnk/b3fb0c1fa8b48dab2bdf to your computer and use it in GitHub Desktop.
AngularJS directive that enables creating 'nugets' - html sections that may be embedded in several places within your template.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define("directives/nuget", ["app"], function (app) { | |
var nugets = []; | |
return app.directive('nuget', [function () { | |
return { | |
restrict: "E", | |
transclude: true, | |
link: function (scope, elm, attrs, ctrl, transclude) { | |
nugets.push({ | |
name: attrs.name, | |
data: (attrs.data||"").split(','), | |
transclude: transclude | |
}); | |
} | |
}; | |
}]).directive('nuget', [function () { | |
return { | |
restrict: "A", | |
link: function (scope, elm, attrs) { | |
var nuget = nugets.filter(function (nugetI) { | |
return nugetI.name == attrs.nuget; | |
})[0]; | |
var newScope = scope.$new(); | |
newScope.nugetData = nuget.data.reduce(function (map, attr) { | |
var value; | |
if (attr[0] == '*') { | |
attr = attr.slice(1); | |
value = scope.$eval(attrs[attr]); | |
} | |
else { | |
value = attrs[attr]; | |
} | |
map[attr] = value; | |
return map; | |
}, {}); | |
elm.replaceWith(nuget.transclude(newScope, function () {})); | |
} | |
}; | |
}]); | |
}); |
Author
srfrnk
commented
Aug 19, 2014
nice, i liked the concept, hopefully will use it soon.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment