2016-06-01 8 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/#/studentsで検索してみてください、angularJSはないのルート私に: すべてが今まで大丈夫だったhttp://enrolin.in/test/#/studentSearch/akしかし、ここでに問題がある:私も絶対に正しい結果を返す部分ページを作成し、こちらのページです 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

こんにちは@ akhilesh-khajuria、ネイティブのjavascriptのwindow.location.href = "あなたのURL"が動作します。 Btw、私はangle-ui-routerを使うことを好みます、それはあなたのルータを設定し、制御するより良い方法です。 – FlowerCheng

1

ルーティング設定で指定した名前と同じ名前を使用していません。ルーティング名は "/ studentSearch /:名前ですか?"この関数では "/ studentsSearch /"として使用されています。

命名の問題を修正して、それが動作するはず$location.path("/studentsSearch/" + $scope.name);

$location.url("/studentsSearch/" + $scope.name);を交換してみてください。

私はこれを試しても問題なく動作します。

+0

こんにちは、オシャダ。私はあなたの修正を試みましたが、それは機能しません。そしてstudentDetailsの部分は問題ではありません。それはstudentSearchの部分です。 –

+0

こんにちは@AkhilEshKhajuria、$ location.url( "/ studentsSearch /" + $ scope.name)を置き換えてみてください。 $ location.path( "/ studentsSearch /" + $ scope.name); – Oshadha

+0

:)が試行されました。まだ動作していません。 –

関連する問題