简体   繁体   English

如何使用ui-codemirror和AngularJS刷新CodeMirror?

[英]How to refresh CodeMirror with ui-codemirror and AngularJS?

https://github.com/angular-ui/ui-codemirror https://github.com/angular-ui/ui-codemirror

I need to make a syntax highlighter in angular and save the results ina database. 我需要在angular中制作语法高亮显示并将结果保存在数据库中。

I am using ui-codemirror but I cannot make it to refresh the textarea everytime that I change the "pre" in the docs it says 我正在使用ui-codemirror,但每次我更改文档中的“pre”时我都无法刷新textarea

<textarea ui-codemirror ng-model="x" ui-refresh='isSomething'></textarea>

but I cannot make it work. 但我不能让它发挥作用。

anyone has any idea on how to do this? 有谁知道如何做到这一点?

I had the same issue and could not figure out a solution for a while. 我有同样的问题,暂时无法找到解决方案。

The CodeMirror instance will be updated once a scope variable/function returns true. 一旦范围变量/函数返回true,CodeMirror实例将被更新。 That can be determined by a function doing a calculation on the value being displayed, but in my snippet I have my data updated from the result of a broadcasted event. 这可以通过对正在显示的值进行计算的函数来确定,但在我的片段中,我从广播事件的结果更新了我的数据。 I set the scope variable to be checked to true, and then with a short delay, I change it back to false. 我将要检查的范围变量设置为true,然后使用短延迟,我将其更改为false。

$scope.$on(ART_EVENTS.updateOverview, function (event, data) {
    $log.info("received overviewData in articleController.");

    // do stuff with the data
    // ...

    $scope.refreshCodemirror = true;
    $timeout(function () {
      $scope.refreshCodemirror = false;
    }, 100);
});

And then in the view: 然后在视图中:

<textarea ui-codemirror ng-model="x" ui-refresh='refreshCodemirror'></textarea>

I do realise that there might be better solutions, but this works for what I need. 我确实意识到可能有更好的解决方案,但这适用于我需要的东西。

Thank you alot. 非常感谢。 Im a angularJS noob and I solved it. 我是一个有角度的新手,我解决了它。 For other newbies. 对于其他新手。 Make sure you inject $timeout beforehand. 确保事先注入$ timeout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM