Skip to content

Instantly share code, notes, and snippets.

@mattlanham
Last active May 19, 2016 07:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattlanham/9738979 to your computer and use it in GitHub Desktop.
Save mattlanham/9738979 to your computer and use it in GitHub Desktop.
Really simple directive for http://vitalets.github.io/x-editable/, created this because i wanted to use the popover, and not the inline version provided by the existing angularJS directive. It's really simple to use, just add 'editable' to your modules, then use like: <h2 ng-model="form.name" editable></h2> you can also pass through options i.e.…
(function() {
"use strict";
angular.module("editableModule", ['ng']).directive("editable", function($timeout) {
return {
restrict: "AE",
scope: true,
require:"ngModel",
link: function(scope, element, attrs, ngModel) {
element.editable({
success: function(response, newValue) {
$timeout(function() {
ngModel.$setViewValue(newValue);
ngModel.$render();
});
}
});
scope.$watch(attrs.ngModel, function(newValue) {
element.editable('setValue', newValue);
});
}
};
});
}).call(this);
@PatrickJS
Copy link

I would change your watch to

scope.$watch(attrs.ngModel, function(newValue) {
  $(element).editable('setValue', newValue);
});

I'm not sure what you're using scope.editable for. You're also creating an isolated scope but not using it other than scope.editable. You can set the scope to true if you don't plan on using it but want the scope isolated

@mattlanham
Copy link
Author

Spot on, i've updated the gist accordingly

@PatrickJS
Copy link

also if you include jquery and it's plugins before angular you element should have . editable() so you don't have to wrap it again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment