清除ng-model-options上的debounce

min*_*rse 5 angularjs

我在模型选项中配置了去抖动输入,如下所示:

<input type="text"
       ng-model="searchTerm" 
       ng-keypress="$event.which===13 && search(searchTerm)" 
       ng-model-options="{debounce: 1000}">
Run Code Online (Sandbox Code Playgroud)

然后,我为模型值设置了一个手表:

$scope.$watch('searchTerm', function (term) {
    if (!term || term.length === 0) {
      $scope.clearSearch();
    }
    if (term.length > 3) {
      $scope.search(term);
    }
});
Run Code Online (Sandbox Code Playgroud)

从输入声明中可以看出,如果按下回车键,我已经配置了一个ng-keypress事件来调用我的搜索功能.

有没有办法可以清除去抖动,所以[enter]键按键会清除debounce,允许searchTerm立即更新?

谢谢

Llo*_*iol 1

最简单的解决方案是在编辑器初始化后设置模型:

  editorInit(editor: MonacoStandaloneCodeEditor) {
    const model = monaco.editor.createModel(
      this.getCode(),
      "json",
      monaco.Uri.parse("a://b/foo.json")
    );
    editor.setModel(model);
  }
Run Code Online (Sandbox Code Playgroud)

这是使用 json 模式的 stackblitz 示例:https://stackblitz.com/edit/materia-ngx-monaco-editor-example-gtgxpy ?file=src/app/app.component.ts

据说,首选的方法是使用该模型初始化编辑器,而不是在创建后进行设置......但在检查了 ngx-monaco-editor 源代码后,这种方式是最一致的解决方案。