[英]Validate value of input with angularjs
I have a text input that pulls the value of the window.location.href
, looks for ?=
and strips out the trailing value which is used as the input value. 我有一个文本输入,提取
window.location.href
的值,查找?=
并去除尾随的值,该值用作输入值。 I can't get the validation to check if there is a value after the =
, and display an error if there isn't. 我无法通过验证来检查
=
之后是否有值,如果没有则显示错误。
The full url it is testing is http://localhost:3000/admin/tagger.html?=1234567
, where 1234567
should be the value in the textbox. 它正在测试的完整URL是
http://localhost:3000/admin/tagger.html?=1234567
,其中1234567
应该是文本框中的值。 If there is no numeric value after the =
, show an error. 如果
=
后面没有数字值,则显示错误。
var myApp = angular.module("myApp", []);
myApp.controller("myController", function ($scope, $http) {
$scope.init = function () {
var url = window.location.href;
var accountId = url.substr(url.indexOf("=") + 1);
if ($(".accountNumber").val() !== window.location.href + "?=") {
alert("ERROR");
} else {
$scope.accountNumber = accountId;
}
}
});
If I understood you questions correctly then you can change your code like this. 如果我正确理解了您的问题,则可以像这样更改代码。
$scope.init = function () {
var url = window.location.href;
var urlParts = url.split('?=');
var accountId = urlParts[1];
if (!accountId) {
alert("ERROR");
} else {
$scope.accountNumber = accountId;
}
}
If you configure your routing differently, you can for example change the url to: http://localhost:3000/admin/tagger/1234567 如果以其他方式配置路由,则可以将URL更改为: http:// localhost:3000 / admin / tagger / 1234567
Your routing would look something like this: 您的路由如下所示:
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/admin/tagger/:id', {
templateUrl: '<path to your html template>'
}
});
}]);
And then in your function you can use $routeParams to check the value of :id
like this: 然后在函数中,可以使用$ routeParams来检查
:id
的值,如下所示:
myApp.controller('myController', ['$routeParams', function($routeParams) {
$scope.init = function() {
if ($(".accountNumber").val() !== $routeParams.id) {
alert("ERROR");
} else {
$scope.accountNumber = accountId;
}
}])
Or something along these lines. 或类似的东西。 The principle is: you can get the number that you want to check by calling $routeParams.id (or $routeParams['id']).
原理是:您可以通过调用$ routeParams.id(或$ routeParams ['id'])来获取要检查的号码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.