【问题标题】:How to set a default <ui-view> within another <ui-view>如何在另一个 <ui-view> 中设置默认 <ui-view>
【发布时间】:2016-05-18 03:27:22
【问题描述】:

所以我想要完成的是:当用户点击 index.html 中的 a-tag 时,会导致他们进入状态“博客”,即 ma​​in。 html,我希望 ma​​in.html 中的 ui-view 元素自动显示 home.html

我尝试在“博客”状态下使用abstract: true, template: '&lt;ui-view&gt;,然后为状态“blogs.home”提供一个空 url,以便它自动显示,但不幸的是,它只是让它填充 index.html 的 ui-view 元素。不是我想要的。

我正在尝试将 ma​​in.html 中的 ui-view 元素定位为默认填充 home.html

任何帮助将不胜感激,我可以澄清在我的解释中不一定有意义的任何事情。提前致谢。

这是我的布局:

index.html

<body ng-controller="MainController">

<!-- HEADER -->
<div class="header">
   <div class="title">
      <i style="color: white;" class="fa fa-magic fa-2x"></i>&nbsp;
      <h3>
          <span>AngularJS Blog</span>
      </h3>
   </div>
   <div class="nav-bar">
      <div class="nav-div nav-div-one">
         <a ui-sref="blogs"><i class="fa fa-archive fa-2x"></i></a>
      </div>
      <div class="nav-div nav-div-two">
         <a ui-sref="newblog"><i class="fa fa-pencil fa-2x"></i></a>
      </div>
   </div>
</div>

<!-- VIEWS -->
<div ui-view class="main-fade"><div>

ma​​in.html

<div class="wrapper"> 
    <div class="blog-container" ng-repeat="post in blog" ng-click="readPost(post._id)" ui-sref=".readblog">  
       <p><i class="fa fa-book"></i>&nbsp;{{ post.title }}</p>
       <p>by {{ post.author }}</p> 
       <p>{{ post.pubdate | date }}</p>
       <br>
       <div>
        <p>{{ post.body }}</p>
       </div>
       <br><br>
       <div style="position: absolute; bottom: 10px; right: 10px;">
          <button id="deletePost" ng-click="deletePost(post._id)">Delete Blog</button>
          <button id="readPost" ng-click="readPost(post._id)" ui-sref=".readblog">Read Blog</button>
       </div>
    </div>
</div>

<div ui-view></div>

home.html

    <div class="home">
        <h1>The place to write stuff down.</h1>
    </div>

app.js

var app = angular.module('blog-app', ['ui.router', 'ngAnimate']);

app.config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/blogs/home');

$stateProvider

.state('blogs', {
    url: '/blogs',
    templateUrl: 'views/main.html',
    controller: 'MainController'
})

.state('blogs.home', {
    url: "",
    templateUrl: "views/home.html",
    MainController: "MainController"
})

.state('blogs.readblog', {
    url: "/readblog",
    templateUrl: "views/readblog.html",
    controller: "MainController"
})

.state('newblog', {
    url: '/newblog',
    templateUrl: "views/newblog.html",
    controller: "MainController"
}); 

}); //End Config. OK

【问题讨论】:

    标签: javascript html angularjs angular-ui-router


    【解决方案1】:

    您需要为视图命名才能做到这一点。

    <ui-view name="mainContent"></ui-view>
    

    在你的配置中-

    .state('blogs.home', {
        url: '/newblog',
        views: {
            'mainContent@blogs': {
                templateUrl: "views/home.html",
                controller: "MainController"
             }
        }
    });
    

    【讨论】:

    • 那行得通。我得到它来显示home.html。但是,当我单击 readPost 按钮时,有没有办法将新视图注入该 ui-view 元素?因为现在这并不像它应该的那样工作。 :(
    • 如果您想替换该视图,您应该始终将您的视图指向mainContent@blogs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2017-02-09
    相关资源
    最近更新 更多