【问题标题】:Two ng-click function not executing together两个 ng-click 函数没有一起执行
【发布时间】:2016-02-16 07:10:00
【问题描述】:

我想一键执行两个函数,但是没有执行。

当我尝试单独执行它们时,它工作正常。

我用分号 ' 分隔两个函数名; '。

这是我的代码:

<!DOCTYPE html>
<html>
<body ng-app="starter" ng-controller="NavCtrl">
<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">Ionic head</h1>
  </ion-header-bar>
  <ion-content>
    <h4>Enter the details</h4>
    <div class="list list-inset" >
      <label class="item item-input">
        <input type="text" name="place" ng-model="data.place" value="" placeholder="Place">
      </label>
      <label class="item item-input">
        <input type="number" name="pincode" ng-model="data.pincode" value="" placeholder="Pin Code">
      </label>
      <button class="button button-block button-positive" ng-click="savedata(); navigat('/success.html')" >
        Submit
      </button>
    </div>
  </ion-content>
</ion-pane>
</body>
</html>

这是控制器代码:

app.controller('NavCtrl',function($scope,$location,$http,$window){
  $scope.data = {};
  $scope.savedata = function(){
    $http.post("http://localhost/angular/insert2.php",{'place':$scope.data.place,'pincode':$scope.data.pincode})
      .success(function(data,status,headers,config){
    //console.log(data);
        alert("successful");
      })
      .error(function(){
        alert("failed");
      })
  };

  $scope.navigat = function(url){
    $window.location.href=url;
  };

  $scope.submit = function(){
    $scope.savedata();
  //$scope.navigat('/success.html');
 // $window.location.href='/success.html';
  }

});

我也尝试调用提交函数,该函数调用了另外两个函数——savedata() 和 navigat()——但它只调用了一个函数。

我还尝试将这两个函数放在两个不同的控制器中——然后在 body 元素上调用一个控制器,在按钮元素之前的 div 元素上调用另一个控制器。但看起来它甚至没有加载一个控制器。

请帮忙

【问题讨论】:

    标签: javascript jquery angularjs ionic-framework


    【解决方案1】:

    你可以试试这个:

       app.controller('NavCtrl',function($scope,$location,$http,$window){
      $scope.data = {};
      $scope.savedata = function(){
        $http.post("http://localhost/angular/insert2.php",{'place':$scope.data.place,'pincode':$scope.data.pincode})
          .success(function(data,status,headers,config){
        //console.log(data);
            alert("successful");
            $scope.navigat('/success.html');
          })
          .error(function(){
            alert("failed");
          })
      };
    
      $scope.navigat = function(url){
        $window.location.href=url;
      };
    
      $scope.submit = function(){
        $scope.savedata();
      //$scope.navigat('/success.html');
     // $window.location.href='/success.html';
      }
    
    });
    

    【讨论】:

      【解决方案2】:

      你可以把navigat('/success.html')放在savedata中post调用的成功回调里面

      【讨论】:

        【解决方案3】:

        您应该在 savedata 成功内调用导航。

        因为当您的savedata 被调用并等待ajax 响应时,同时navigate 被调用并导航到新页面。

        如下使用回调

        $scope.savedata = function(callback){
            $http.post("http://localhost/angular/insert2.php",{'place':$scope.data.place,'pincode':$scope.data.pincode})
              .success(function(data,status,headers,config){
                alert("successful");
                if(callback) callback($scope);
              })
              .error(function(){
                alert("failed");
              })
          };
        

        如下调用

        <button class="button button-block button-positive" ng-click="savedata(function (s) {s.navigat('/success.html')} );" >
                Submit
              </button>
        

        http://plnkr.co/edit/QF80s7YxsLor70cNxLRk

        【讨论】:

        • 谢谢我给了'$scope.navigat('/success.html');在 $scope.savedata() .success 中,刚刚从 ng-click 调用 savedata()。它现在工作
        • 如果您需要更改此导航 URL,但您的保存数据相同,那么您可以按照我的方法。
        • 实际上,这种方法对我不起作用。它没有重定向到成功页面。
        • 我需要使用另一个功能几乎相似的函数 getdata,我在这两种方法中都做过,但它不能双向工作!它没有重定向。请帮忙
        • 你可以在回调函数中尝试$scope.navigate
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-21
        • 2013-07-09
        • 2012-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多