简体   繁体   English

在表单上按下提交时如何调用不同的get函数; 角

[英]How to call different get functions when submit is pressed on the form; angular

I have a three functions in each there is $scope.fetch() which is being called when ng-submit=fetch is pressed. 我每个都有三个函数,当按下ng​​-submit = fetch时会调用$ scope.fetch()。 I want one ng-submit button which would call all three fetch() in three functions how can I do this? 我想要一个ng-submit按钮,它将在三个函数中调用所有三个fetch(),我该怎么做? HTML: HTML:

  <div type="text/ng-template" id="getnewcoolios.html" class="users">
          <h1>{{message}}</h1>
<form name="myform" id="myform1" ng-submit="fetch()" >
<input type="date" 
   ng-model="date"  
   value="{{ 'date' | date: 'dd/MM/yyyy' }}" />
<div><center><button type="submit" >Fetch</button></center></div>
</form>
{{formdata.date}}
<ul ng-controller="NewCooliosCtrl" ng-repeat="newCoolio in newCoolios.newCoolios">
  <li>{{newCoolio.personID}},  {{newCoolio.placeID}}, {{newCoolio.datePlaced}}</li>
</ul>
 <ul ng-controller="NewPlacesCtrl" ng-repeat="newPlace in newPlaces.newPlaces">
  <li>{{newPlace}} </li>
  </ul>
 <ul ng-controller="NewUsersCtrl" ng-repeat="newUser in newUsers.newUsers">
  <li>New Users: {{newUser}} </li>
 </ul>

                 </div>

Angular: 角度:

  myApp.config(['$routeProvider',
 function($routeProvider) {
  $routeProvider
.when('/getdailydata', {
    templateUrl: 'templates/getnewcoolios.html',
    controller: 'DailyCtrl'
  })
}])
     .controller('DailyCtrl', function($scope) {
 })
 .controller('NewUsersCtrl', function($scope,$http,$filter) {
  $scope.fetch= function(){

var formdata =
{'date' : $filter('date')(this.date, 'dd/MM/yyyy')
};

var inserturl = 'http://94.125.132.253:8001/getnewusers?date=' + formdata.date;

$http.get(inserturl).success(function (data) {
console.log(formdata);
 $scope.newUsers = data;
 console.log(inserturl);
 console.log(data);
  $scope.message = 'List of New Users';
  })}
   })
 .controller('NewPlacesCtrl', function($scope,$http,$filter) {

$scope.fetch= function(){

 var formdata =
 {'date' : $filter('date')(this.date, 'dd/MM/yyyy')
 };

var inserturl = 'http://94.125.132.253:8001/getnewplaces?date=' + formdata.date;

 $http.get(inserturl).success(function (data) {
console.log(formdata);
$scope.newPlaces = data;
console.log(inserturl);
console.log(data);
$scope.message = 'List of New places';
}
)

}
})
.controller('NewCooliosCtrl', function($scope,$http,$filter) {

  $scope.fetch= function(){

var formdata =
{'date' : $filter('date')(this.date, 'dd/MM/yyyy')
 };

var inserturl = 'http://94.125.132.253:8001/getnewcoolios?date=' + formdata.date;

 $http.get(inserturl).success(function (data) {
console.log(formdata);
$scope.newCoolios = data;
console.log(inserturl);
console.log(data);
$scope.message = 'List of New Coolios';
 }
 )}});

You can access outer scope properties in inner scopes, so you can define each fetch function in inner scopes and push them into array of functions to be called on submit. 您可以访问内部作用域中的外部作用域属性,因此可以在内部作用域中定义每个访存函数,并将它们推入要在提交时调用的函数数组。 Then, on submit^ just iterate through that array and call each function. 然后,在Submit ^上迭代该数组并调用每个函数。 Voila. 瞧。

<div ng-controller="fetcher">
    <form name="myForm" ng-submit="fetch"></form>
    <ul ng-controller="NewCooliosCtrl"></ul>
    <ul ng-controller="NewPlacesCtrl"></ul>
    <ul ng-controller="NewUsersCtrl"></ul>
<div>

.controller('fetcher', function($scope){
    $scope.toFetch = [];
    $scope.fetch = function(){
        for(var i=0; i<$scope.toFetch.length; i++){
             $scope.toFetch[i]();
        }
    }
});
.controller('NewCooliosCtrl', function($scope){
    $scope.fetch= function(){...};
    $scope.toFetch.push($scope.fetch);
})
.controller('NewPlacesCtrl', function($scope){
    $scope.fetch= function(){...};
    $scope.toFetch.push($scope.fetch);
})
.controller('NewUsersCtrl', function($scope){
    $scope.fetch= function(){...};
    $scope.toFetch.push($scope.fetch);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM