javascript - 如何使 AngularStrap typeahead 选择不可编辑

标签 javascript angularjs twitter-bootstrap angular-strap

我正在使用 AngularStrap 的 typeahead 元素:

<input type="text" ng-model="selectedFruit" bs-options="fruit for fruit in fruits" placeholder="Begin typing fruit" bs-typeahead>

当在 typeahead 中选择水果时,它会在 typeahead 中显示为用户可以编辑的字符串。我希望用户只能通过确切名称提交水果。如果用户选择 apple 然后不小心将字符串编辑为 aple 我的应用程序将在他们提交时崩溃。

有没有办法让字符串在被选中后在预输入中不可编辑?用户应该能够通过从 typeahead 数组中选择另一个水果来更改他们的选择,因此第一个选择不应该是不可更改的。

最佳答案

因此,根据上面 Luke 的建议,以下解决方案对我有用:

HTML:

<div ng-repeat="pie in pies">
    <input type="text" bs-on-select="addSpelling" ng-blur="spellCheck()" bs-options="stock for stock in allBaskets" ng-model="pie.fruit" bs-typeahead>
</div>

注意它需要是 bs-on-select="addSpelling" 而不是 bs-on-select="addSpelling()" 因为第二个选项会导致页面加载时要触发的函数。

在 Controller 中:

$scope.addSpelling = function(){
    this.scope.spelling = this.scope.$modelValue;
}

$scope.spellCheck = function(){
    if(this.pie.fruit == ""{
        return;
    }
    if(this.pie.fruit != this.spelling){
        this.pie.fruit = this.spelling;
    }    
}

关于javascript - 如何使 AngularStrap typeahead 选择不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39030305/

相关文章:

javascript - 如何在 Protractor 中获取元素可见性的 bool 值并在其他代码执行之前返回它?

css - 使用媒体查询将 CSS 类设置为等于 BootStrap 类

JavaScript原型(prototype).js : How to pass parameter to callback function

javascript - 使用 jQuery 显示 wordpress 帖子

javascript - 如何删除 iFrame 中的某个元素

angularjs - 带有 blob 和 WebAPI 的 Angular FileSaver。下载的PDF是空白的。但是 API url 返回带有内容的 PDF

javascript - 为什么我的代码没有将所有内容绘制到 Canvas 上?

javascript - 页面转换无法正常工作

css - bootstrap 第二轮播 其他 css 规则

jquery - 两个并排的流体div