2016-08-23 66 views
0

我正在嘗試使用url參數導航到angularjs應用中的不同視圖。我已經試過這樣的事情角度更改視圖url參數

<a href="" ng-click="goToDevice(treeCurrentNode.uuid)">{{treeCurrentNode.displayName}}</a> 

JS:使用

$location.path('/devices').search({resourceView: 'deviceDetailsView'}) 
       .search({explorerView: 'table'}).search({type: 'Device'}).search({uuid: uuid}); 

而且像這樣的東西$stateProvider

<a ui-sref="Devices({resourceView: 'deviceGroupView', explorerView: 'table', type: 'Device', uuid: treeCurrentNode.uuid})">{{treeCurrentNode.displayName}}</a> 

我的狀態提供的代碼如下所示:

.state('Devices', { 
    url: "/devices", 
    params: { 
     resourceView: null, 
     explorerView: null, 
     type: null, 
     uuid: null 
    }, 
    templateUrl: "app/main/devices/module/views/devices.html", 
    controller: "DevicesExtendedCtrl", 
    reloadOnSearch: false, 

我也是Ø嘗試使用$stateProvider使用$state.go這樣

<a href="" ng-click="go(treeCurrentNode.uuid)">{{treeCurrentNode.displayName}}</a> 

JS:

$state.go('Devices', {resourceView: 'deviceDetailsView', explorerView: 'table', type: 'Device', uuid: uuid}) 

但參數沒有被添加到URL。該網址我心有所屬時,我不嘗試添加的參數是這樣的:

$location.path('/devices') 

默認情況下在那裏的ResourceView和explorerView參數,而不是類型和UUID。

回答

0

我打電話$location.search()錯了,我把它改成$location.search('uuid', uuid)和它的工作,沒有必要修改stateProvider

0

請嘗試更改您的stateprovider代碼,如下所示,看看它是否有效。它對我有用。

.state('Devices', { 
    url: "/devices/:resourceView/:explorerView/:type/:uuid", 
    params: { 
     resourceView: null, 
     explorerView: null, 
     type: null, 
     uuid: null 
    }, 
    templateUrl: "app/main/devices/module/views/devices.html", 
    controller: "DevicesExtendedCtrl", 
    reloadOnSearch: false, 
+0

即將關閉。我應該提到,當我不嘗試添加任何參數時,我指向的默認url已經將resourceView和explorerView添加爲參數。當我改變這樣的代碼時,它具有resourceView和explorerView作爲參數,但它在參數前面將上下文路徑添加了type和uuid – gary69

0

我假設你正在使用的用戶界面,路由器,所以你應該定義在$ stateProvider url財產。它看起來像這樣:

$stateProvider 
    .state('contacts.detail', { 
     url: "/contacts/:contactId", 
     templateUrl: 'contacts.detail.html', 
     controller: function ($stateParams) { 
      // If we got here from a url of /contacts/42 
      expect($stateParams).toBe({contactId: "42"}); 
     } 
    }) 

在這個例子中,我們定義了contactId參數。 /contact/123應該匹配你的網址。

使用ui-router定義參數時,可以使用$ stateParams.contactId。

更多細節,你可以找到https://github.com/angular-ui/ui-router/wiki/URL-Routing#url-parameters