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. 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. 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
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:
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']).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.