Javascript 在AngularJS中手动调用$q
从我对AngularJS中的Javascript 在AngularJS中手动调用$q,javascript,angularjs,Javascript,Angularjs,从我对AngularJS中的$q的理解来看,我刷新页面时会运行一切。这类似于将函数放入ng init 下面是my$q.allcode $q.all([$scope.getCart(), $scope.getCategory(), $scope.getMenu(), $scope.getRestaurant()]).then(function(data){ $scope.cart = data[0].cart; $scope.cartItems = data[0].cartItem
$q
的理解来看,我刷新页面时会运行一切。这类似于将函数放入ng init
下面是my$q.all
code
$q.all([$scope.getCart(), $scope.getCategory(), $scope.getMenu(), $scope.getRestaurant()]).then(function(data){
$scope.cart = data[0].cart;
$scope.cartItems = data[0].cartItems;
$scope.delDay = data[0].delDay;
$scope.delTime = data[0].delTime;
$scope.cat = data[1];
$scope.itemData = data[2];
$scope.rest = data[3];
});
正如您所看到的,每当我刷新页面并将数据添加到我的$scope
时,所有函数都会被调用。这是意料之中的事
我的问题是如何通过下面的另一个函数调用$q
$scope.notEnoughTime = function () {
if (year = 2015) {
// invoke $q manually thereby calling
// all the functions and updating all the data in my scopes
}
}
正如@Scott提到的,您可以将调用放在函数中。有了函数中的请求,您可以随时调用该函数
var serverRequest = function() {
$q.all()
}
$scope.notEnoughTime = function() {
if (year = 2015) {
serverRequest();
}
}
// if you'd like the call the request when the controller is initialized you simple add the function call to your controller
serverRequest();
如果“刷新页面”意味着整个页面正在重新加载,比如单击浏览器上的“刷新图标”,那么只需将$q.all(..)放入函数中,在控制器底部(最后一件事)调用该函数,然后在NoteNowTime中,您可以再次调用该函数。