2017-09-13 94 views
1

點擊編輯按鈕後,我正在重定向到編輯頁面。重定向到下一頁後無法獲取數據

<button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ng-click="editData(n.id.userId)"><span class="glyphicon glyphicon-pencil"></span></button> 

點擊此按鈕下面的代碼工作。

$scope.editData = function (data, $window) { 
    console.log(data); 
    window.location = 'update-data?id=' + data; 
} 

現在我重定向到URL www/xxx/update-data?id=1。 從那裏我想獲取ID = 1的用戶數據,並希望在頁面顯示。 如何做到這一點?

回答

0

可以使用$window獲取URL和做根據您的需要

var url = "www/xxx/update-data?id=1"; 
 
var id = url.substring(url.indexOf('=')+1); 
 
console.log(id); 
 
var url = "www/xxx/update-data?id=1"; 
 
var id = url.substring(url.indexOf('?')+1); 
 
console.log(id);

但最好的選擇(做事情的角度方式)以下將做路由之一併根據您的路由器使用$routeParams$stateparams

相關鏈接

  1. Configure Routes and Views using $routeProvider
  2. Documentation ngRoute
  3. UI-Router for AngularJS (1.x)
  4. Get Current URL
+0

我這樣做如上所述是正確的,或者我必須使用ng路由 – xrcwrn

+0

如果你沒有那麼多的路由,我想上面的方法將罰款。但是如果你的應用程序有這麼多頁面,那麼最好使用路由。 – Vivz

+0

好的,讓我先試試 – xrcwrn

0

可以解析URL並獲得ID和編寫代碼來獲取數據相應的內部ngOninit()