angular中的$ scope模型不会自行更新

时间:2014-04-25 14:58:11

标签: javascript jquery angularjs autocomplete

我一直在使用角度,事情就是我把这个ng-model作为文本输入,同时我用这个自动完成指令,问题是我想改变它一旦从自动完成中选择某个输入,输入的值,而只是在我需要的时候输入数字就是描述,那就是当我把所有数字放在一起时它会按照我想要的方式改变它,有人会帮忙吗我,这是代码    $ scope.trailer = {

options: {
    minLength:2,
    html: true,
    source:"controllers/search.php?catalogo=trailers",

    select: function(event, ui){
      console.log(ui);       
      //$(this).val(ui.item.desc+' '+ui.item.clave).change();
      $scope.formData.trailerid = ui.item.id;
      $scope.formData.trailerclave = ui.item.clave;
      $scope.formData.trailerdueno = ui.item.propietario;
      $scope.formData.trailer = ui.item.desc;//+' '+ui.item.clave; 

      return false;
    },
    change: function (event, ui) {
      if (ui.item == null) {
          $scope.formData.trailerid = 0 ;
          $scope.formData.trailerdueno = '';
          $scope.formData.trailerclave = '';
          $scope.formData.trailer = '';
      }
    }
},
methods: {}

 }

和html

<input type="text" class="form-control trailer" ng-model="formData.trailer" ui-autocomplete = "trailer" />
                                        <span class="help-block">{{formData.trailer}}</span>

1 个答案:

答案 0 :(得分:0)

有时候我发现我使用的函数或事件是外部插件的一部分而不是与Angular有关我需要在$ scope中包装任何内容。$ apply(function(){});

options: {
minLength:2,
html: true,
source:"controllers/search.php?catalogo=trailers",

select: function(event, ui){
 $scope.$apply(function(){
  console.log(ui);       
  //$(this).val(ui.item.desc+' '+ui.item.clave).change();
  $scope.formData.trailerid = ui.item.id;
  $scope.formData.trailerclave = ui.item.clave;
  $scope.formData.trailerdueno = ui.item.propietario;
  $scope.formData.trailer = ui.item.desc;//+' '+ui.item.clave; 
 });
  return false;
},
change: function (event, ui) {
  if (ui.item == null) {
    $scope.$apply(function(){
      $scope.formData.trailerid = 0 ;
      $scope.formData.trailerdueno = '';
      $scope.formData.trailerclave = '';
      $scope.formData.trailer = '';
    });
  }
}
},
methods: {}

 }