AngularJS中的typeahead指令来获取链输入自动填充



我现在正在尝试使用angularJS编写表达式,这就像数学表达式一样。但是当我试图向运营商提出建议时,我卡住了。

例如,表达式可能如下所示:ABS(x)+Floor(y)。

我尝试使用预键入指令,但建议仅显示 ABS(x)。输入运算符"+"和 Floor(y) 时,没有建议。这是我的代码快照。如果有人能在这方面提供帮助,我将非常高兴。谢谢!

.JS:

$scope.explist = ['ABS(x)',  'Floor(y)']

.HTML:

<div>
<input type="text" maxlength="100" ng-model="exp" typeahead="e for e in explist | filter:$viewValue | limitTo:8"  />
</div>

阅读您的评论后,我认为在您的情况下使用提前输入是错误的东西。

-

->见JSBIN<--

我建议使用 ui-select。您可以在一个数组中列出表达式,然后使用所选内容构建第二个数组。

由于您需要使用多个运算符,因此您可以为指令提供一个显示可用选项的函数。在这个例子中,我已经给了他们

var modifiers = ['/', '+', '-', '*'];

这是控制器

    $scope.exp.available = ['ABS(x)', 'Floor(y)'];
    $scope.exp.formatted = function () {
        var holder = [];
        angular.forEach($scope.exp.available, function (thing) {
          holder.push(thing);
          for (i = 0; i < modifiers.length; i++) {
                    holder.push(modifiers[i] + thing);
                }
        });
        return holder;
    };

.HTML

<ui-select multiple ng-model="exp.expressions" theme="select2" style="width: 300px;">
    <ui-select-match placeholder="Select...">{{$item}}</ui-select-match>
    <ui-select-choices repeat="e in exp.formatted() | filter:$select.search">
      {{e}}
    </ui-select-choices>
  </ui-select>

您将需要角度清理和 ui 选择。我在这里为您创建了一个JSBinhttp://jsbin.com/paqotiticu/2/edit?html,css,js,output

这只是一个开始,您可以开发使用按键或删除元素的功能,希望这有所帮助,祝您好运!

最新更新