構文を使用してng-metadata
にディレクティブを変換しようとしていますが、特定の部分を見つけられません。この指示にはelement.html()
が必要ですが、新しい指示にどこが当てはまるのかわかりません。ディレクティブ内の要素から "html"を取得する
これは指令です。
angular.module('ngContenteditable', ['ngSanitize']).
directive('contenteditable', ['$sce','$sanitize', function($sce, $sanitize) {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
if (!ngModel) return;
ngModel.$render = function() {
element.html($sanitize(ngModel.$viewValue || ''));
};
element.on('blur keyup change', function() {
scope.$apply(readViewText);
});
function readViewText() {
var html = element.html();
if (attrs.stripBr && html == '<br>') {
html = '';
}
ngModel.$setViewValue(html);
}
}
};
}]);
これまでのところ、これはng-metadataバージョンのものです。
import { Directive, Inject, Self, Optional, Input, HostListener } from 'ng-metadata/core';
@Directive({
selector: '[contenteditable]'
})
export class ngDirective {
@Input() stripBr: string;
constructor(
@Inject('ngModel') @Self() @Optional() private ngModel: any,
@Inject('$sce') private $sce: ng.ISCEService,
@Inject('$sanitize') private $sanitize: ng.sanitize.ISanitizeService) {
}
@HostListener('blur keyup change')
onNgChange() {
}
onReadViewText() {
let html = this.html(); // ... how do I get the HTML?
}
}
$ elementサービスと$ element.html()が必要です。 ng-metadataに固有のものではありません。これはコードがリンク関数からコントローラに移動して戻ったときに行われる処理です。 – estus
しかし '$ element'はどこから来たのですか? – Ciel
依存性注入から。 https://docs.angularjs.org/api/ng/service/$compile#-controller- – estus