2016-06-01 55 views
2

我学习角传递,所以这里是我的testapp:http://enrolin.in/test/#/studentsAngularJS无法正常路由当链接从功能

现在,这里我想通过名称来搜索数据库。所以我创建了PHP,它返回我所需要的。这里是php:http://enrolin.in/test/login.php?p=fetchbyname&&name=ak 你必须将URL中的名称替换为你需要搜索的任何东西。我还创建了返回完全正确的结果的部分页面,这里是页:http://enrolin.in/test/#/studentSearch/ak 一切都很好至今不过现在的问题是:

当我尝试在http://enrolin.in/test/#/students搜索,angularJS不路由我像http://enrolin.in/test/#/studentSearch/ak 而是与我在$routeProvider

这里已经设置为我angularJS(我已删除了一些不重要的代码)默认:

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; 
            }) 
     } 
     }) 

以前每次我都想把链接放到html路由中,我曾经写过像<a href="#/courses">courses</a> 但现在当我想把它放在函数中时,我不知道该写什么。请帮忙。

回答

1

我想你可以使用window.location.href而不是标签来重定向。或者你可以使用“哈希状态”来控制你的路由器func

+0

嗨FlowerChng,谢谢你的回复。但是我对角度很陌生,你能告诉我如何编写代码吗? –

+1

Hi @ akhilesh-khajuria,原生javascript window.location.href =“您的网址”可以工作。 顺便说一句,我更喜欢使用angular-ui-router,它是配置和控制路由器的更好方法。 – FlowerCheng

1

你没有使用你在路由配置中提到的相同名称。路由名称是“/ studentSearch /:名称?”但你已经在功能中使用了“/ studentsSearch /”。

请尝试用$location.path("/studentsSearch/" + $scope.name);

正确的命名问题,更换$location.url("/studentsSearch/" + $scope.name);,它应该工作。

我试过这个,它工作正常。

+0

你好,奥沙哈。我只是尝试了你的修复,但它不工作。而studentDetails部分不存在问题,我认为它是studentSearch部分。 –

+0

Hi @AkhilEshKhajuria,请尝试替换$ location.url(“/ studentsSearch /”+ $ scope.name);用$ location.path(“/ studentsSearch /”+ $ scope.name); – Oshadha

+0

:)尝试过。它仍然不起作用。 –