【问题标题】:ng-model not working with typeaheadng-model 不能与 typeahead 一起使用
【发布时间】:2014-10-22 15:29:39
【问题描述】:

我正在使用 Angular 的 typeahead,但遇到了 ngModel 的问题。

这是我的预输入 html

<input
    type= "text"
    ng-model= "symbol"
    placeholder= "begin typing"
    typeahead= "hit.message for hit in getTypeAheadContents($viewValue)"
    typeahead-loading= "loadingSymbols"
    typeahead-editable= "false"
    typeahead-on-select= "onSelect($item, $model, $label)"
    typeahead-min-length= 2
    typeahead-wait-ms= 500
    class= "form-control" 
/>

<input ng-click= "search()" value= "Search!"/>

这是我的控制器中的代码(暂时很基础)

$scope.search = function(){
    alert($scope.symbol);
}

现在,自动完成代码按预期工作,但是当我单击搜索按钮时,我收到警告消息“未定义”

更奇怪的是我尝试设置

 $scope.symbol = "";

在我的控制器的开头,当我点击搜索按钮而不在我的预输入中输入任何内容时,我收到了空字符串提醒我,正如预期的那样。但是,当我在预输入中输入一些内容并再次点击搜索时,我再次返回“未定义”。很明显,angular 的 typeahead 与 ng-model 配合得不是很好,但我不知道该怎么做。

建议?

【问题讨论】:

    标签: angularjs angularjs-scope undefined typeahead angular-ngmodel


    【解决方案1】:

    只需设置typeahead-editable="true" :-)

    【讨论】:

    • typeahead-editable的默认值为true。
    • 但是在本题的html代码中设置为false
    【解决方案2】:

    不知道这对您来说是否仍然是个问题。但是我已经尝试了 angularStrap (2.1.4) 的最新版本,当我将 ng-model 设置为我设置属性的对象时,我得到了它的工作。

    $scope.selectedPart = {}
    
          <input type="text"
                 class="form-control"
                 ng-model="selectedPart.part_id"
                 data-animation="am-flip-x"
                 ng-options="part.value as part.name for part in parts"
                 placeholder="Selecteer onderdeel"
                 bs-typeahead>
    

    在某个函数的某个地方(可能是一个很深的 $watch)

    console.log($scope.selectedPart.part_id)
    

    【讨论】:

    • 我为自己找到了解决办法,但感谢您回复我。我很感激。
    【解决方案3】:

    这并没有真正回答这个问题,但我通过在 getTypeAheadContents 函数中设置一个与用户输入相等的不同范围变量,然后在搜索功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多