gpt4 book ai didi

javascript - AngularJS Uncaught ReferenceError : response is not defined

转载 作者:行者123 更新时间:2023-11-29 19:22:40 34 4
gpt4 key购买 nike

大家好,我是 AngularJS 的新手。我正在尝试调试其他人的代码,但收到一条错误消息,我不确定如何修复。错误信息是 Uncaught ReferenceError: response is not defined。这是我的 Controller :

(function () {

angular
.module('vidaexpress')
.controller('accountManagerController', accountManagerController);

accountManagerController.$inject = ['$state', 'accountManagerService', 'toastr'];

function accountManagerController($state, accountManagerService, toastr) {
var vm = this;
vm.updatePassword = updatePassword;
vm.updatePhone = updatePhone;

init();

function init(){
getCustomerInfo();
}

function getCustomerInfo() {
accountManagerService.getCustomerInfo().then(function (response) {
vm.customerInfo = response;
}, function (error) {
vm.error = error;
});
}

function updatePassword(password) {
accountManagerService.updatePassword(password).then(function (response) {
if (response.error) {
displayError(response.error);
} else {
$state.go('main.manage.index');
}
}, function (error) {
displayError(response.error);
});
}

function updatePhone(phone) {
accountManagerService.updatePhone(phone).then(function (response) {
if (response.error) {
displayError(response.error);
} else {
$state.go('main.manage.index');
}
}, function (error) {
displayError(response.error);
});
}

function displayError(error) {
toastr.error(error, 'Error');
}
}

})();

这是我的服务。如果需要任何其他信息,请告诉我 (函数(){

angular
.module('vidaexpress')
.service('accountManagerService', accountManagerService);

accountManagerService.$inject = ['$http', 'apiUrl'];

function accountManagerService($http, apiUrl) {
var baseUrl = apiUrl.account;
this.updatePassword = updatePassword;
this.updatePhone = updatePhone;
this.getCustomerInfo = getCustomerInfo;

function getCustomerInfo() {
return $http.get(baseUrl + 'getCustomerInfo').then(function (response) {
return response.data;
}, function (error) {
});
}

function updatePassword(password) {
return $http.post(baseUrl + 'updatePassword', password)
.then(function (response) {
return {};
}, function(error) {
return { error: error.data.message };
});

}

function updatePhone(phone) {
return $http.post(baseUrl + 'updatePhone', phone)
.then(function (reponse) {
return {};
}, function (error) {
return { error: error.data.message };
});
}
}

})();

这是我的看法:

form class="form form-vertical" id="new_pass" name="new_pass" ng-submit="new_pass.$valid && vm.updatePassword(vm.password)" novalidate>
<div class="form-group" ng-show="vm.updatePasswordError">
<span class="alert alert-danger col-lg-12">{{vm.updatePasswordError}}</span>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<label for="currpw" class="control-label">{{'PASSWORD_ENTER_CURRENT' | translate }}</label>
<input id="currpw" class="form-control" title="New Password" required="" type="password" ng-model="vm.password.oldPassword">
</div>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<label for="currpw" class="control-label">{{ 'PASSWORD_ENTER_NEW' | translate }}</label>
<input id="signuppassword" name="signuppassword" type="password" class="form-control" autocomplete="off" required ng-model="vm.password.newPassword" ng-pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,18}$/" ng-focus="vm.signuperror = '';">
<div class="error_msg" ng-show="new_pass.signuppassword.$error.required && submit">{{ 'PASSWORD_ENTER' | translate }}</div>
<div class="error_msg" ng-show="new_pass.signuppassword.$error.pattern && submit">
{{ 'PASSWORD_ERROR_PATTERN' | translate }}
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-lg-12">
<label for="newpw" class="control-label">{{ 'PASSWORD_ENTER_NEW_CONFIRM' | translate }}</label>
<input id="confirmpassword" name="confirmpassword" type="password" class="form-control" required ng-model="vm.password.confirmpassword" ve-compare-to="vm.password.newPassword" ng-focus="vm.signuperror = '';">
<div class="error_msg" ng-show="new_pass.confirmpassword.$error.required && submit">{{ 'PASSWORD_ENTER_CONFIRM' | translate }}</div>
<div class="error_msg" ng-show="new_pass.confirmpassword.$error.veCompareTo && submit">{{ 'PASSWORD_ERROR_MATCH' | translate }}</div>
</div>
</div>
</div>

<div class="row">
<div class="col-lg-4 col-lg-12 buttonblocks">
<button type="submit" class="btn btn-primary button_dkblue_lg" ng-click="submit=true;">{{ 'UPDATE' | translate }}</button>
</div>
<div class="col-lg-8 col-xs-12 buttonblocks align-right">
<a ui-sref="main.manage.index" class="btn btn-primary button_dkblue_lg">{{ 'CANCEL' | translate }}</a>
</div>
</div>

<style>
.far_right{float:right;}
.pwalert { margin-top: 29px;}

最佳答案

在您的错误回调中,您有时会使用 response 而您应该使用 error

updatePassword 示例:

function updatePassword(password) {
accountManagerService.updatePassword(password).then(function (response) {
// ...
}, function (error) {
// displayError(response.error); <-- this must be your error:
// there is no response variable in current scope
displayError(error);
});
}

关于javascript - AngularJS Uncaught ReferenceError : response is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32483735/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com