简体   繁体   English

我可以在hrefRouterProvider中使用href吗?

[英]Can I use href with urlRouterProvider?

In my angularjs app I have replaced ngRoute (routeProvider) with ui.router (urlRouterProvider) module and stateProvider to navigate between states within my app. 在我的angularjs应用程序中,我已用ui.router(urlRouterProvider)模块和stateProvider替换了ngRoute(routeProvider),并在应用程序的状态之间进行导航。

Now I find out that ui-router only works with states and <a ui-sref=""> instead of <a href=""> . 现在,我发现ui-router仅适用于状态和<a ui-sref="">而不是<a href=""> As I have to set the application navigation outside of my app by using javascript, I can not change the anchors from href to sref. 由于必须使用javascript在应用程序外部设置应用程序导航,因此无法将锚点从href更改为sref。 Here's how I set navigation links. 这是我设置导航链接的方法。

    Portal.setNavigation(
        [
            {
                "label": "Application1",
                "selected": true,
                "url": "/web/guest#/Application1"
            }
        ]
    );

This code set's the href attributes accordingly. 该代码集相应地具有href属性。 Here is my stateProvider snippet, which works on manually placed srefs like this one: <a ui-sref="Application1">Application State 1</a> 这是我的stateProvider代码段,它适用于像这样的手动放置的sref<a ui-sref="Application1">Application State 1</a>

app.config(['$stateProvider', '$urlRouterProvider',
  function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('Application1', {
            url: '/Application1',
            templateUrl: '/Application.html',
            controller: 'AppCtrl'
        });
}]);

Any ideas how I can continue using ui-router AND using hrefs to navigate through states? 有什么想法可以继续使用ui-router和使用href导航状态吗?

If you are navigating outside of your application through javascript by doing something like window.location(url), you should be able to to put your state's route in the url and the router should be able to handle the route ie window.location(/state1). 如果您通过执行诸如window.location(url)之类的操作来通过javascript导航到应用程序之外,则应该能够将您所在州的路由放入url中,并且路由器应该能够处理该路由,即window.location(/ state1)。

Here's the text from the ui-router that sheds some light on this ( link ): 这是ui-router的文本,它对此有所启发( link ):

Here's how you set a basic url. 这是设置基本网址的方法。

 $stateProvider .state('contacts', { url: "/contacts", templateUrl: 'contacts.html' }) 

Now when the user accesses index.html/contacts then the 'contacts' state would become active and the main ui-view will be populated with the 'contacts.html' partial. 现在,当用户访问index.html / contacts时,“ contacts”状态将变为活动状态,并且主用户界面视图将填充“ contacts.html”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM