0
所以,我想要的是一个自定义指令,它将读取并清除当前的选择,然后将选定的文本传递给回调函数。这是有效的,但无论我在回调函数中做什么都没有影响范围,这导致我认为有多个范围,它们在某种程度上有冲突。从AngularJS中的指令回调函数改变模型没有效果
首先,我定义了这样的指令:
angular.module('app').directive('onTextSelected', ['$window', function ($window) {
return {
restrict: 'A',
scope: {selectFn: '&'},
link: function (scope, element, attrs) {
$(element).mouseup(function() {
var selection = $window.getSelection().toString();
if ($window.getSelection().removeAllRanges) {
$window.getSelection().removeAllRanges();
} else if ($window.getSelection().empty) {
$window.getSelection().empty();
}
if (selection && selection.trim() !== "") {
scope.selectFn({
text: selection.trim()
});
}
});
}
};
}]);
它在模板中使用如下:
<pre ng-bind-html="message" id="messagePre" on-text-selected
select-fn="textSelected(text)"></pre>
这是回调函数:
$scope.textSelected = function (text) {
console.log(text);
$scope.currentText = text;
};
我有一个文本框,它使用$scope.textSelected
作为模型,并使用来自另一个函数wor的相同代码进行设置ks正确,但在这种情况下,它没有。尽管所有代码都被执行(例如,它在控制台上打印),但没有任何反应。