Last active
August 29, 2015 14:13
-
-
Save EvilScott/cfef50a7d6f5ab61d633 to your computer and use it in GitHub Desktop.
Angular Inline Editing Directive
This file contains 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
<div class="edit-inline"> | |
<span ng-show="editing"> | |
<input type="text" ng-model="text" /> | |
<button ng-click="editing = false">Save</button> | |
</span> | |
<span ng-hide="editing"> | |
<span ng-bind="text"></span> | |
<button ng-click="editing = true">Edit</button> | |
</span> | |
</div> |
This file contains 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
(function() { | |
'use strict'; | |
angular | |
.module('editInline', []) | |
.directive('editInline', function() { | |
return { | |
link: function(scope, _ele, _attrs, _ctrl, transcludeFn) { | |
var localStorageKey = 'editInline::' + scope.name; | |
scope.$watch('text', function(newVal) { | |
localStorage.setItem(localStorageKey, newVal); | |
}); | |
transcludeFn(function(content) { | |
var localStorageValue = localStorage.getItem(localStorageKey); | |
if (localStorageValue) { | |
scope.text = localStorageValue; | |
} else { | |
scope.text = content[0] ? content[0].textContent : 'default text'; | |
} | |
}); | |
}, | |
restrict: 'E', | |
scope: { name: '@' }, | |
templateUrl: 'edit-inline.html', | |
transclude: true | |
}; | |
}); | |
})(); |
This file contains 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
<!DOCTYPE html> | |
<html ng-app="editInline"> | |
<head> | |
<title>Edit Inline</title> | |
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" /> | |
</head> | |
<body> | |
<!-- demo usage --> | |
<edit-inline name="foo"></edit-inline> | |
<edit-inline name="bar">default bar text</edit-inline> | |
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.min.js"></script> | |
<script type="text/javascript" src="/editInline.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment