2016-06-01 52 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

:)嘗試過。它仍然不起作用。 –