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正確,但在這種情況下,它沒有。儘管所有代碼都被執行(例如,它在控制檯上打印),但沒有任何反應。