2016-08-22 70 views
0

我已經使用$ routeProvide將頁面從一個頁面重定向到另一個頁面,並且我在url中傳遞了一些動態參數。

它在一個地方工作,不與另一個地方工作。

我的代碼:在app.js

app.config(['$routeProvider', '$locationProvider', 
function ($routeProvider, $locationProvider) { 
    $routeProvider 
      .when('/', { 
       templateUrl: "client/home/home.html", 
       controller: "IndexCtrl", 
       access: { 
        isloggedIn: false 
       } 
      }) 
      .when('/test/:searchTerm', { 
       templateUrl: "client/test.html", 
       controller: "testController", 
       access: { 
        isloggedIn: false 
       } 
      }) 
      .when('/test2/:id', { 
       templateUrl: "client/index.html", 
       controller: "indexController", 
       access: { 
        isloggedIn: true 
       } 
      }) 

      .otherwise({ 
       redirectTo: "/" 
      }); 
}]); 
在控制器文件

:對於時候的TestController我打網址 server.com/#/test/45在控制檯45和indexController的了,當我打URL服務器.com /#/ test2/45然後我得到:控制檯中的id。

app.controller("testController", ['$location', '$rootScope', '$routeParams', function ($location, $rootScope, $routeParams) { 
console.log($routeParams.searchTerm); //get result 45 
}]); 

app.controller("indexController", ['$location', '$rootScope', '$routeParams', function ($location, $rootScope, $routeParams) { 
console.log($routeParams.id); //get result :id 
}]); 

爲什麼發生這種情況有人可以幫忙嗎?應該是什麼問題?

+0

是URL路徑:表示 '/ TEST2/ID'是正確的? –

+0

分享你的html也 –

+0

@ebinmanuva我已經把只有測試文本都在HTML和兩者都存在的位置 – Nitin

回答

0

嘗試($routeParams.id);,而不是($routParams.id);,我不知道爲什麼你的$routParams.searchTerm工作,但它不應該(應該是route,不rout)。

+0

我在我的例子中使用了$ routeParams ..它只是拼錯了。感謝點我在那裏我已更新我的問題 – Nitin

0

似乎你的代碼是正確的,除了拼寫錯誤。我能找到的唯一區別是IsLoggedIn屬性值。你試過把它弄錯了嗎? IsLoggedIn屬性的意義何在?

+0

在我的情況下,我已經使用額外的參數訪問基礎,如果有一些網頁只用於登錄用戶,當我試圖做我登錄我的系統 – Nitin

+0

你是越來越瀏覽器控制檯中的任何錯誤?你能分享一下index.html的html嗎? –

0

查看您的hyperlink的路線/test2/:id。 它可能會像ng-href="#/test2/:id" 改變它ng-href="#/test2/{{id}}"


不要忘記定義變量id

重建的問題fiddle