【问题标题】:$scope model in angular is not updating itself角度中的 $scope 模型不会自行更新
【发布时间】:2014-06-11 08:55:00
【问题描述】:

我一直在使用 Angular,问题是我将这个 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>

【问题讨论】:

    标签: javascript jquery angularjs autocomplete


    【解决方案1】:

    有时我发现如果我使用的函数或事件属于与 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: {}
    
     }
    

    【讨论】:

    • 什么也没发生 :(,你认为我必须把 ui-autocomplete 指令放在里面吗?还是有办法在控制器中初始化范围?谢谢你的好意
    • 谢谢,显然问题出在更改功能上,我刚刚添加了这个'$scope.formData.trailer = ui.item.desc;'首先,非常感谢您的回答
    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多