如何隐藏和显示模板而不是每次都销毁和重新注入它们

时间:2015-03-03 01:37:26

标签: angularjs angular-ui-router

我正在使用ui-router和Named视图。它完美无瑕,但它不适合我的目的。我不希望每次点击菜单项时都会注入模板,但只是在加载应用程序后第一次点击它时。剩下的时间,它应该只是隐藏模板并显示当前为菜单点击激活的模板。当回到原始菜单点击时,它应该只是做ngShow =" true"隐藏模板并隐藏当前模板。没有编辑angular-ui-router中的cleanupLastView()可以做到吗?

如果我注释掉cleanupLastView(),它只会继续注入模板而不对旧模板做任何事情。所以最终都显示出来。试图调试cleanupLastView(),但我无法弄清楚我可以使用哪些变量,所以我可以重新显示第二次点击的内容而不是再次注入。

的index.html

<!DOCTYPE html> 
<html lang="en"> 
<head ng-app="myApp"> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link href="style.css" rel="stylesheet"> 
    <script type="text/javascript" src=".lib/angular-1.3.0/angular.js"></script> 
    <script type="text/javascript" src="./angular-ui-router.js"></script> 
    <script type="text/javascript" src="./modular/ct-ui-router-extras.core.js"></script> 
    <script type="text/javascript" src="./modular/ct-ui-router-extras.dsr.js"></script> 
    <script type="text/javascript" src="../timerService.js"></script> 
    <script type="text/javascript" src="../dataService.js"></script> 
    <script type="text/javascript" src="app.js"></script> 
</head> 
<body> 
<div class="navbar navbar-default navbar-static-top" role="navigation"> 
    <ul class="nav navbar-nav"> 
        <li ng-class="{active: $state.includes('main')}"><a ui-sref="home">Main</a></li> 
        <li ng-class="{active: $state.includes('files')}"><a ui-sref="files">Files</a></li> 
    </ul> 
</div> 
<div> 
    <div ui-view="pane"></div> 
</div> 
</div> 
</body> 
</html>

app.js

var myApp = angular.module("myApp", [ 'ui.router', 'ct.ui.router.extras.dsr']); 
app.config(function ($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise("/"); 
        $stateProvider
    .state('main',
    url:"/main",
             deepStateRedirect: true,
    views : {
        pane: {
            templateUrl : 'tpl.pane-0.html'
            },
        }
    )
.state('files',
    url: '/files',
             deepStateRedirect: true,
    views : {
        pane : {
            templateUrl : 'tpl.pane-1.html'
            },
        'first@files' : {
            templateUrl : 'tpl1.first.html',
                              deepStateRedirect: true
            },
        'second@files' : {
            templateUrl : 'tpl1.second.html',
                              deepStateRedirect: true
            },  
        },
    )
  });

tpl.pane-0.html

<div>
    <input type='text'></input>
</div>

tpl.pane-1.HTML

<div >
    <div data-ui-href='first'></div>
    <div data-ui-href='second'></div>
</div> 

tpl1.first.html

<div>
    <input type='text'></input>
</div>

tpl1.second.html

<div>
Second
</div>

这是我的应用的简化代码。它可以用于执行多个命名视图,但在deepStateRedirect失败,这似乎是与导航栏类型的模板相关联而不是(?)Sticky。那是对的吗?我将param deepStateRedirect:true添加到所有正在注入的视图中但无效。如果写入了tpl1.first.html的输入文本框并且视图切换为main,并且返回,则在输入框中找不到文本。我做错了什么?

2 个答案:

答案 0 :(得分:1)

是的,你可以这样做,但需要一些自定义工作。

  • 更改状态,使其使用空模板。
  • 在州的控制器中检查文档是否已包含视图的HTML(使用唯一ID)。
  • 如果需要,请使用$template缓存和$compile创建原始模板视图,并将其附加到文档正文中。
  • 在模板中,您可以使用ng-show="isState_expression | isState"表达式来切换模板的可见性。

基本上ui-routes的工作方式和以前一样,但是你自己处理模板的编译。一旦它进入DOM(不在ui-view内)。您可以将其留在那里由ng-show处理。

我不建议在状态上使用resolve,因为它会变成静态。

这是$compile上的手册:

https://docs.angularjs.org/api/ng/service/ $编译

这是$compile的教程:

http://www.benlesh.com/2013/08/angular-compile-how-it-works-how-to-use.html

这是$compile的答案:

Compiling dynamic HTML strings from database

答案 1 :(得分:1)

为什么它不适合您的需求?当你没有出现时你需要保留状态吗?

如果是这样,也许你应该研究UI Router Extras Sticky States。它允许您保留状态的$scope,而不是每次都创建/销毁它。这比尝试重新发明轮子要好。

看看the sample