मेरा लक्ष्य एक editable
निर्देश एक उपयोगकर्ता जो करने के लिए विशेषता जुड़ा हुआ है किसी भी तत्व का HTML संपादित करने की अनुमति देता है कि बनाने के लिए है (Plunker देखें: http://plnkr.co/edit/nIrr9Lu0PZN2PdnhQOC6)कोणीय निर्देश के भीतर मूल transcluded सामग्री प्राप्त करें
यह लगभग को छोड़कर काम करता है मुझे पाठ क्षेत्र को प्रारंभ करने के लिए स्थानांतरित सामग्री का मूल कच्चा HTML नहीं मिल सकता है। मैं इसे clone.text()
से टेक्स्ट प्राप्त कर सकता हूं, लेकिन इसमें <H1>
, <div>
इत्यादि जैसे एचटीएमएल टैग गायब हैं, इसलिए कोई संपादन के साथ आवेदन पर क्लिक करना बेवकूफ नहीं है।
विधि clone.html()
एक त्रुटि फेंकता है, Cannot read property 'childNodes' of undefined
app.directive("editable", function($rootScope) {
return {
restrict: "A",
templateUrl: "mytemplate.html",
transclude: true,
scope: {
content: "=editContent"
},
controller: function($scope, $element, $compile, $transclude, $sce) {
// Initialize the text area with the original transcluded HTML...
$transclude(function(clone, scope) {
// This almost works but strips out tags like <h1>, <div>, etc.
// $scope.editContent = clone.text().trim();
// this works much better per @Emmentaler, tho contains expanded HTML
var html = "";
for (var i=0; i<clone.length; i++) {
html += clone[i].outerHTML||'';}
});
$scope.editContent = html;
$scope.onEdit = function() {
// HACK? Using jQuery to place compiled content
$(".editable-output",$element).html(
// compiling is necessary to render nested directives
$compile($scope.editContent)($rootScope)
);
}
$scope.showEditor = false;
$scope.toggleEditor = function() {
$scope.showEditor = !$scope.showEditor;
}
}
}
});
(यह सवाल अनिवार्य रूप से, पहले के एक सवाल फ्रेम करने के लिए प्रयास के बाद सवाल और कोड की एक थोक पुनर्लेखन है Get original transcluded content in Angular directive)
'क्लोन' तत्वों का संग्रह है। क्या आप इसे डीबगर में देख सकते हैं? –
आह! उन पर इटरेट करना और बाहरी HTML जोड़ना बहुत करीब है: 'var text = ""; के लिए (var i = 0; i 'के बजाय यह' 'दिखाता है। इस उदाहरण में, घड़ी सामग्री को स्थानांतरित नहीं करती है, इसलिए शुद्ध प्रभाव समान होता है। मुझे आश्चर्य है कि मूल HTML प्राप्त करना संभव है? –
prototype
Clock
{{time}}
मुझे संदेह था कि यह मामला हो सकता है। अच्छा सौदा। मूल HTML बाहरी दायरे में $ तत्व ऑब्जेक्ट में बैठा हो सकता है। परिवर्तन मेरा मजबूत सूट नहीं है। –