Javascript 从函数传递链接时AngularJS未正确路由
我正在学习Angular,以下是我的测试: 现在我想按名称搜索数据库。所以我创建了一个php,它返回我所需要的内容。以下是php: 您必须将url中的名称替换为需要搜索的任何内容。我还创建了一个返回绝对正确结果的部分页面,页面如下: 到目前为止一切都很好,但问题是: 当我尝试搜索时,angularJS不会将我发送到 而是我在Javascript 从函数传递链接时AngularJS未正确路由,javascript,php,angularjs,routing,Javascript,Php,Angularjs,Routing,我正在学习Angular,以下是我的测试: 现在我想按名称搜索数据库。所以我创建了一个php,它返回我所需要的内容。以下是php: 您必须将url中的名称替换为需要搜索的任何内容。我还创建了一个返回绝对正确结果的部分页面,页面如下: 到目前为止一切都很好,但问题是: 当我尝试搜索时,angularJS不会将我发送到 而是我在$routeProvider 这是我的angularJS(我删除了一些不重要的代码): 以前每次我想在html中添加一个链接到我以前编写的路由,就像 但是现在,当我想把它放在
$routeProvider
这是我的angularJS(我删除了一些不重要的代码):
以前每次我想在html中添加一个链接到我以前编写的路由,就像
但是现在,当我想把它放在函数中时,我不知道该写什么。请提供帮助。我认为您可以使用window.location.href而不是标记来重定向。或者,您可以使用“哈希状态”在func中控制您的路由器您使用的名称与您在路由配置中提到的名称不同。路由名称为“/studentSearch/:name?”,但您在函数中使用了“/studentSearch/” 请尝试替换
$location.url(“/studentsSearch/”+$scope.name)代码>带有$location.path(“/studentsSearch/”+$scope.name)代码>
纠正命名问题,它应该可以工作
我试过这个,效果很好。你好,谢谢回复。但是我对angular很陌生,你能告诉我如何编写代码吗?嗨@akhilesh khajuria,本地javascript窗口。location.href=“你的URL”可以工作。顺便说一句,我更喜欢使用AngularUI路由器,这是配置和控制路由器的更好方法。你好,Oshadha。我刚试过你的办法,但没用。studentDetails部分没有问题,我认为是studentSearch部分。嗨@AkhilEshKhajuria,请尝试替换$location.url(“/studentsSearch/”+$scope.name);使用$location.path(“/studentsSearch/”+$scope.name);我想我不明白这个问题。你能给我详细解释一下吗?尝试一下我在上一个评论解决方案中提出的建议:当(“/studentSearch/:name?”,{templateUrl:“templates/studentSearch.html”,controller:“studentSearchController”})Oshadha,我刚才在这里又问了一个问题:看看是否有帮助
var app = angular
.module("Demo", ["ngRoute"])
.config(function ($routeProvider) {
$routeProvider
.when("/students/:id", {
templateUrl: "templates/studentDetails.html",
controller: "studentDetailsController"
})
.when("/studentSearch/:name", {
templateUrl: "templates/studentSearch.html",
controller: "studentSearchController"
})
.otherwise({
redirectTo: "/home"
})
})
.controller("studentDetailsController", function ($scope, $http, $routeParams) {
$http({
url: "http://enrolin.in/test/login.php?p=fetchone&&id=",
method: "get",
params: { id: $routeParams.id }
}).then(function (response) {
$scope.stud = response.data;
})
})
.controller("studentsController", function ($scope, $http, $route,$location) {
$scope.searchStudent=function(){
if($scope.name){
$location.url("/studentsSearch/" + $scope.name);
}
else{
$location.url("/studentsSearch/");
}
}
$scope.reloadData=function(){
$route.reload();
}
$http.get("http://enrolin.in/test/login.php?p=fetchall")
.then(function (response) {
$scope.students = response.data;
})
})
.controller("studentSearchController", function ($scope, $http, $routeParams) {
if($routeParams.name)
{
$http({
url: "http://enrolin.in/test/login.php?p=fetchbyname&&name=",
method: "get",
params: { name: $routeParams.name }
}).then(function (response) {
$scope.studs = response.data;
})
}
else
{
$http.get("http://enrolin.in/test/login.php?p=fetchall")
.then(function (response) {
$scope.students = response.data;
})
}
})