我目前正在开发一个AngularJS应用程序,允许用户激活他们的帐户,输入用户数据并在完成时提交JSON数据。
我想要实现的目标:
如果用户数据有效(在JSON数据中)在提交时重定向到'/ form /',则显示无效的错误消息'无效帐户'。
当前问题:
1. 如果用户首先输入正确的成员资格和激活号码,则用户将重定向到'/ form /',但如果用户输入了错误的详细信息,则输入正确的详细信息,重定向不起作用。
2。我尝试对if else语句实施无效警报,但同时在4/5警报内收到警报,不确定为什么会发生这种情况?
我正在Plnkr上开发这个项目,请随时帮助并指出我的正确方向。谢谢。
http://plnkr.co/edit/5uPSn2ae0yFjYzujPFWm?p=preview
$scope.findMembership = function() {
angular.forEach($scope.membershipData.membershipNumber, function(value, key) {
if (key === $scope.membershipValue && value[0].activationNumber === $scope.activationValue) {
$location.path("/form/");
}
});
};
更新
formCtrl.controller('activation', function($scope, $location, $rootScope) {
var normalized = Object.keys($scope.membershipData.membershipNumber).map(function(k) {
return { key : k, val : $scope.membershipData.membershipNumber[k][0].activationNumber }
});
normalized = [
{"key":"541","val":"541X"},
{"key":"4692","val":"4692X"},
{"key":"45165","val":"45165X"},
{"key":"5464565","val":"5464565X"},
{"key":"54645651","val":"54645651X"},
{"key":"D4554160N","val":"D4554160NX"}
]
$scope.findMembership = function() {
if (normalized.some(function(o) {
return o.key == $scope.membershipValue && o.val == $scope.activationValue
})) $location.path("/form/")
}
});
答案 0 :(得分:0)
尝试将您的其他块更改为:
otherwise({
templateUrl: 'view/activation.html',
controller: 'activation'
});
我认为重定向导致应用无法找到它正在寻找的模板。另外,我要避免在angular.forEach循环中使用$ location.path()方法。也许可以将数据规范化为真正的数组,这样就可以使用Array.some()了。
这样的事情:
var normalized = Object.keys($scope.membershipData.membershipNumber).map(function(k) {
return { key : k, val : $scope.membershipData.membershipNumber[k][0].activationNumber }
});
/*
normalized = [
{"key":"541","val":"541X"},
{"key":"4692","val":"4692X"},
{"key":"45165","val":"45165X"},
{"key":"5464565","val":"5464565X"},
{"key":"54645651","val":"54645651X"},
{"key":"D4554160N","val":"D4554160NX"}
]
*/
$scope.findMembership = function() {
if (normalized.some(function(o) {
return o.key == $scope.membershipValue && o.val == $scope.activationValue
})) $location.path("/form/")
}