在Inline Edit中,我们正在尝试复制粘贴自己的内容,但它会自行复制html内容。
请找到以下屏幕截图:
如何避免复制html内容和样式?
我们只需要复制粘贴内容。
HTML
<td><a href="" contentEditable="true" ng-model="fName">John</a></td>
Angular:
var myApp = angular.module('myApp', []);
myApp.directive('contenteditable', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
// view -> model
elm.bind('blur', function() {
scope.$apply(function() {
ctrl.$setViewValue(elm.text());
});
});
// model -> view
ctrl.render = function(value) {
elm.html(value);
};
// load init value from DOM
ctrl.$setViewValue(elm.text());
elm.bind('keydown', function(event) {
console.log("keydown " + event.which);
var esc = event.which == 27,
el = event.target;
if (esc) {
console.log("esc");
ctrl.$setViewValue(elm.text());
el.blur();
event.preventDefault();
}
});
}
};
})
CSS:
a[contentEditable] {
cursor: pointer;
background-color: white;
padding: .2em;
}
a[contentEditable]:focus {
cursor: pointer;
background-color: #D0D0D0;
border: 1px solid red;
padding: .2em;
}