1

我正在使用Angular Bootstrap UI typeahead尝试更新 typeahead 中的模型值。但除非特别应用,否则更新的模型值不会反映更改。

<input ng-model="query" typeahead="option for option in getOptions($viewValue)">

app.controller('MainCtrl', function($scope) {
  $scope.query = '';
  $scope.getOptions = function(query){
    if(query == "foo"){
      $scope.query = "foobar";
      // uncommenting the next line works but also raises error
      // $scope.$apply()
      return []
    }
    return ["I have an idea", "I have new idea"];
  }
});

Plunker 代码: http ://plnkr.co/edit/pNxBMgeKYulLuk7DO0gB?p=preview

4

1 回答 1

1

您可以通过观察 的值来完成此操作$scope.query

其中$watch,newValue和分别表示改变前后oldValue的值。$scope.query

$scope.$watch("query", function(newValue, oldValue){
  if (newValue !== oldValue) {
    if (newValue === "foo") {
      $scope.query = "foobar";
    }
  }
});

http://plnkr.co/edit/Fsj8KWuSzr9VTMkKB9wO?p=preview

于 2013-09-20T20:34:31.103 回答