【问题标题】:Angular and code mirror角度和代码镜像
【发布时间】:2014-08-01 08:44:56
【问题描述】:

我一直在尝试向javascript 工具添加一些功能,我需要突出显示codemirror 中的一行。我正在使用AngularJs。 首先,我尝试按照教程进行操作

function highlightLine(lineNumber) {

    //Line number is zero based index
    var actualLineNumber = lineNumber - 1;

    //Select editor loaded in the DOM
    var myEditor = $("#body_EditorSource .CodeMirror");

    //Write the item to the console window, for debugging
    console.log(myEditor);

    //Select the first item (zero index) just incase more than one found & get the CodeMirror JS object
    var codeMirrorEditor = myEditor[0].CodeMirror;

    //Write the item to the console window, for debugging
    console.log(myEditor[0].CodeMirror);

    //Set line CSS class to the line number & affecting the background of the line with the css class of line-error
    codeMirrorEditor.setLineClass(actualLineNumber, 'background', 'line-error');
}

但它不起作用,没有检测到myEditor var...我试过了:

var myEditor = document.getElementById('codeMirrorDiv');
var myEditor = angular.element(document.querySelector((".codeMirrorDiv")));

什么都没有。似乎是什么问题?

【问题讨论】:

    标签: javascript angularjs dom codemirror


    【解决方案1】:

    函数调用addLineClass

    【讨论】:

    • 是的,在我注意到愚蠢的错误 a 这样做之后://行号是从零开始的索引 var actualLineNumber = lineNumber - 1; //选择DOM中加载的编辑器 var myEditor = angular.element(document.querySelector(("#codeMirrorDiv"))); var codeMirrorEditor = myEditor[0].childNodes[0].CodeMirror; codeMirrorEditor.addlineClass(actualLineNumber, 'background', 'line-error');仍然没有工作
    • 您在控制器中究竟在哪里使用此代码?指示?在什么“时间”。
    • 我正在使用控制器的功能,当用户选择结果扫描时,它会显示 var 行突出显示蓝色。 show_result(){ ... setvalue() //填充代码镜像 setcursor() //设置光标 sethighlight(3) //只是为了测试,hihglihg line blue }
    • 那么我们需要更多的上下文,你能做一个显示问题的小提琴吗?因为我的猜测是你选错了。您不应该在控制器中使用 jquery,而应该创建一个指令。
    • 事情在codeMirrorEditor.addlineClass(actualLineNumber, 'background', 'line-error');在调试时,codemirroreditor 有 proto 中的功能:对象,但为什么我无法访问它? jsfiddle.net/utcsz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多