答案 0 :(得分:0)
可能存在针对此的指令,但这是一个简单的例子:
app.directive("richEditor", function() {
return {
restrict: "A",
scope: {
variables: "=",
ngModel: "="
},
template: "<div>\
<textarea ng-model='ngModel'></textarea>\
<span ng-repeat='variable in variables' ng-click='varSelected(variable)'>{{ variable }}</span>\
</div>",
link: function(scope, element, attrs) {
var textarea = element.find("textarea");
scope.varSelected = function(variable) {
if (!scope.ngModel) {
scope.ngModel = "";
}
var position = textarea[0].selectionStart;
var val = scope.ngModel.substring(0, position) + variable + scope.ngModel.substring(position);
scope.ngModel = val;
}
}
}
});