javascript - 如何限制用户在 ui-ace 编辑器中只写一个 javascript 函数(方法)

标签 javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

这是我的 Controller 代码

  $scope.aceOptions = {
      workerPath: 'http://localhost:50871/Scripts/app/vendor/',     
      useWrapMode: true,
      showGutter: true,
      theme: 'chrome',
      firstLineNumber: 1,
      onLoad: function (_editor) {
          $scope.script = {};
          $scope.script.scriptCode = "function fieldName_columnName_" + "functionName(){\n\n\n}";             
          var _session = _editor.getSession();
          _session.setMode('ace/mode/javascript');
          var _renderer = _editor.renderer;

          _session.on('changeAnnotation', function () {
              $scope.annotations = [];
              $scope.annotations = _editor.getSession().getAnnotations();
          }),
          _editor.$blockScrolling = Infinity;

          _session.on("change", function (e) {               
              var currentValue = _editor.getSession().getValue();
              $scope.script.scriptCode = currentValue;
          });


      },
      require: ['ace/ext/language_tools'],
      advanced: {
          enableSnippets: true,
          enableBasicAutocompletion: true,
          enableLiveAutocompletion: true
      }
  }

我为 ui-ace 写了指令这是我的 html 代码

 <javascript-editor code="script.scriptCode" ace-option="aceOptions"></javascript-editor>

指令代码是

SCSApp
.directive('javascriptEditor', [
    function () {          
        return {
            restrict: 'E',
            scope: {
                data: '=code',
                aceOption: '='
            },
            templateUrl: '/Scripts/app/shared/directives/javascripEditor/partials/javascriptEditor.html',
            link: function (scope, element, attrs) {
            }
        }
    }]);

这是我的 javascriptEditor.html

<div class="e1" ui-ace="aceOption" ng-model="data"></div>

我只是想限制用户在 ui-ace 编辑器中不能写一个以上的 javascript 函数

最佳答案

您可以创建自己的 javascript 简单函数解析器或使用类似 parser3 的库

然后编写逻辑规则,根据ACE编辑器的内容向UI显示成功/失败。

它像编译器一样解析字符串/句子以创建低级代码。

关于javascript - 如何限制用户在 ui-ace 编辑器中只写一个 javascript 函数(方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259683/

相关文章:

javascript - 带 A 限制的指令的属性

Javascript 过滤功能无法正常工作

javascript - ng-repeat 位于 ng-repeat 内部,元素作为 sibling

javascript - 为什么我的指令在使用 AngularJS 1.5 时第一次使用 ng-if 运行时没有进入动画?

javascript - 转换为 Base64 AngularJS 时获得异步函数

angularjs - Angular 凹坑错误 TypeError : Cannot read property '_hasCategories' of null

javascript - 允许多个主题标签的输入模式,以空格分隔 (#one #two)

javascript - 使用其他页面中的 'href' (到达)取消隐藏特定 div

javascript - Strapi - 获取上次内容更改日期

unit-testing - 在angularjs中测试指令时如何模拟模糊?