如何在<ui-view>另一个<ui-view>中设置默认值



所以我试图实现的是:当用户点击index.html中的a标签,该标签将引导他们进入"博客"状态,即main.html时,我希望main.html中的ui视图元素自动显示home.html

我曾尝试在"blogs"状态中使用abstract: true, template: '<ui-view>,然后给状态"blogs.home"一个空url,以便它自动显示,但不幸的是,这只是为了填充index.html的ui视图元素。不是我想要的。

我正在尝试将main.html中的ui视图元素作为目标,并将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>

main.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

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

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

在你的配置-

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

最新更新