2016-08-23 63 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