2014-07-10 39 views
1

我有三個函數,每個都有$ scope.fetch(),當ng-submit = fetch被按下時被調用。我想要一個ng-submit按鈕,它會在三個函數中調用所有三個fetch(),我該怎麼做? HTML:如何在表單上按提交時調用不同的get函數; angular

<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> 

角:

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'; 
} 
)}}); 
+0

如果您使用空格進行縮進而不是製表符,它可能會幫助他人閱讀您的代碼。 – RobG

回答

0

可以在內部範圍訪問外部範圍的特性,所以可以定義在內部範圍的每個取功能,並將它們推入的函數陣列被稱爲上提交。然後,在提交^只是遍歷該數組並調用每個函數。瞧。

<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); 
}) 
+0

我將此添加到我的代碼中,但它不起作用,還有什麼我需要做的。我對角度很陌生 – butYouDontLookLikeADeveloper

相關問題