簡體   English   中英

在angularjs中使用ui-router

[英]Using ui-router in angularjs

在遷移到ui-router之前,我正在使用ngRoute

在我的應用程序中,我有這個配置:

myapp.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: 'views/home.html'
    })
    .state('about', {
      url: '/about',
      templateUrl: 'views/about.html'
    })
    .state('contact', {
      url: '/contact',
      templateUrl: 'views/contact.html'
    })
    .state('work', {
      url: '/work',
      templateUrl: 'views/work.html'
    })
    .state('work.top_authors', {
      url: '/top_authors',
      templateUrl: 'views/work/top_authors.html',
      controller: 'topAuthorsController'
    })
    .state('work.articles_evolution', {
      url: '/articles_evolution',
      templateUrl: 'views/work/articles_evolution.html',
      controller: 'articlesEvolutionController'
    })
    .state('work.top_organizations', {
      url: '/top_organizations',
      templateUrl: 'views/work/top_organizations.html',
      controller: 'topOrganizationsController'
    })
    .state('work.words_cloud', {
      url: '/words_cloud',
      templateUrl: 'views/work/words_cloud.html',
      controller: 'wordsCloudController'
    })
    .state('work.authors', {
      url: '/authors',
      templateUrl: 'views/work/authors.html',
      controller: 'authorsController'
    })
    .state('work.words', {
      url: '/words',
      templateUrl: 'views/work/words.html',
      controller: 'wordsController'
    })
    .state('work.jobs_offers', {
      url: '/jobs_offers',
      templateUrl: 'views/work/jobs_offers.html',
      controller: 'JobOffersByCounteryController'
    });
  $urlRouterProvider.otherwise('/home');
});

對於前4個州,我沒有任何問題,但對於其他工作狀態的孩子,我有這個問題:

因此,當我點擊(例如) <a ui-sref="#/work/articles_evolution">Artciles Evolution</a>我在控制台中收到此錯誤:

Error: Could not resolve '#/work/articles_evolution' from state 'work'

但是當我在瀏覽器中鍵入鏈​​接時: http://localhost/dst-angular/app/#/work/articles_evolution它沒有問題。

我該如何解決這個問題?

我的第二個問題是:

在html頁面work我有<div ui-view></div> ,它將被ui-sref的模板替換,我希望這個頁面包含一些內容然后當我點擊ui-sref來替換嵌入新模板時的此內容。

我怎樣才能做到這一點 ?

你應該在url的ui-sref instread中傳遞州名:

<a ui-sref="work.articles_evolution">Artciles Evolution</a>

ui-sref指令將從狀態名稱解析url並設置為錨標記的href屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM