我有这个Angular函数多次触发,我很困惑,为什么它会这样做。在后端,我有一个console.log()语句,每次点击端点时都会记录。声明发射4次。 instagramService.tapInsta(); 应该只能被调用一次。有人能告诉我为什么以及这是怎么发生的?如何防止Angular方法多次触发?
'use strict';
console.log("OUTSIDE alloyController");
angular.module("mainModule")
.controller('alloyController', function ($scope, instagramService) {
console.log("INSIDE alloyController");
$scope.windowInfoWithToken = instagramService.getWindowInfo();
instagramService.tapInsta($scope.windowInfoWithToken, function (response) {
$scope.instagramData = response.data;
if (!response.data.access_token == undefined) {
$scope.instagramDataWithToken = response.data.access_token;
} else {
$scope.hideThisDiv = true;
}
console.info(response.data);
});
});
在回答一个评论,我有实际的API调用服务完成的:
'use strict';
console.log("OUTSIDE dataService");
angular.module("mainModule")
.service('instagramService', function ($http) {
console.log("INSIDE dataService");
this.tapInsta = function (access_token, callback) {
$http({
method: 'POST',
url: '/ig',
data: {
token: access_token
}
})
.then(callback);
}
this.tapInstaExtended = function (access_token, instaQuery, callback) {
$http({
method: 'POST',
url: '/instaInputQuery',
data: {
token: access_token,
query: instaQuery
}
})
.then(callback);
}
this.getHandleAuth = function (callback) {
$http({
method: 'GET',
url: '/handleauth',
data: {
name: "LTQ"
}
})
.then(callback);
}
this.getWindowInfo = function() {
var windowLocation = window.location.href;
if (windowLocation.indexOf("losethequit") != -1) {
var windowLocationWithToken = windowLocation.replace("https://losethequit.herokuapp.com/views/werkspayce.html?code=", "");
} else {
var windowLocationWithToken = windowLocation.replace("http://localhost:5000/views/werkspayce.html?code=", "");
}
return windowLocationWithToken;
};
});
多少次是在页面上使用该控制器? – aaronofleonard