Angularjs,Range.insertNode,ngClick:$ compile之后Angular不知道元素 - ngClick不工作

时间:2016-05-26 04:01:30

标签: angularjs range angularjs-ng-click

在$ compile之后,ngClick仍然没有响应。新元素正在应用于DOM,可以通过jQuery和JS访问。我假设问题在于range.insertNode函数。我在这里错过了什么? 这是我的指示:

.directive('selectText', [
    '$rootScope',
    '$compile',
    '$window', 
    function ($rootScope, $compile, $window) {
    return {
        restrict: 'A',
        scope: {
            hlid: "=",
            tu: "="
        },
        link: function (scope, element, attrs) {
            element.on('mouseup', function () {
                //console.log("Attrs: "+JSON.stringify(attrs));
                if ($window.getSelection().toString()) {
                    var text = $window.getSelection().toString();
                    if(text == '') {
                        console.log("No selection");
                        return;
                    }
                    var selection = $window.getSelection();
                    var range = selection.getRangeAt(0);
                    var selectionContents = range.extractContents();
                    var clk = "edSel('hl_"+scope.hlid+"','"+attrs.id+"');";
                    // var span = $compile(angular.element('<hlight id="hl_'+scope.hlid+'" class="cr-pr noselect clickable" title="Text Selection" ng-click="'+clk+'">'+text+'</hlight>'))(scope);
                    var span = angular.element($compile('<hlight id="hl_'+scope.hlid+'" class="cr-pr noselect clickable" title="Text Selection" ng-click="'+clk+'">'+text+'</hlight>')(scope));
                    console.log(span);
                    range.insertNode(span[0]);
                    scope.tu.target = element.html();
                    //selection.removeAllRanges();
                    var arr = {};
                    arr.action = 'add';
                    arr.tuid = attrs.id;
                    arr.hlid = 'hl_'+scope.hlid;
                    arr.content = element.html();
                    scope.$emit('hlChange', arr);
                    scope.hlid++;
                    console.log(element.html());
                    var modal = UIkit.modal("#hl_modal");
                    modal.show();
                }
            });

            scope.edSel = function(id,tuid) {
                console.log('ID: '+id+" - tuID: "+tuid);
            }
        }
    };
}])

感谢您的帮助

0 个答案:

没有答案