javascript - 如何在选择输入中选择选项之前停止函数调用

标签 javascript angularjs

我在我的选择输入中有一个产品列表,我想要的是每次从列表中选择一个产品时调用“duplicateGammeProduit”,这将发送该产品的 ID,但我注意到的是在我选择任何选项之前调用方法,这是我的代码:

<select ng-model="produitId" ng-change="duplicateGammeProduit(produitId)">
        <option ng-repeat="pi in listProduitUsed" value="{{pi.id}}">{{pi.reference}}</option>
    </select>

这是 Controller 代码:

.controller(
        'GammeCtrl', [
            '$scope',
            '$http',

            function($scope, $http) {

    $scope.duplicateGammeProduit = function(produitId) {
      $http.get(MyURL:" +produitId).success(
           function(gammeProduit) {                                     
          //the method to be called when an option is selected          
      $scope.classifierListElementGamme(gammeProduit.listElementGamme);
           gammeProduit.id = null
             ....
            $scope.finalOperationsList = gammeProduit.listElementGamme;
          });
       }

  $scope.listeProduitUsed = function() {
                $http
                    .get(URL/getProduitListUsed")
                    .success(
                        function(dataProduit) {

                            $scope.listProduitUsed = dataProduit;
                        });}
      $scope.listeProduitUsed();

}]);

所以我怎样才能在选择选项之前停止这个调用,因为这个调用 减慢我的应用程序 谢谢帮助

最佳答案

$scope.duplicateGammeProduit = function(produitId) {
  if (!produitId) {
    return;
  }

  $http.get(MyURL:" +produitId).success(
       function(gammeProduit) {                                     
      //the method to be called when an option is selected          
  $scope.classifierListElementGamme(gammeProduit.listElementGamme);
...

和我的评论一样

关于javascript - 如何在选择输入中选择选项之前停止函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245999/

相关文章:

javascript - 如何依次执行父路由和子路由的解析?

javascript - 调用 karma 测试时未定义 Angular 错误

javascript - AngularJS $http 返回值

javascript - 如何提高 javascript 代码的性能

javascript - 在 match() Javascript 上返回行号

javascript - 使用javascript遍历phantomjs中的网址数组

javascript - ng-click 不会在已编译的模板内触发

javascript - 使用 angular-ui-router 带有参数的动态 templateUrl

javascript - 点击并返回

javascript - 无法让ajax请求在jquery中使用json工作