Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 停止删除视图中的页面_Javascript_Angularjs - Fatal编程技术网

Javascript 停止删除视图中的页面

Javascript 停止删除视图中的页面,javascript,angularjs,Javascript,Angularjs,好的,我需要能够阻止ng-view擦除内容,我需要有ng-view,因为它是唯一一个不会(也不能)破坏整个应用程序的地方 我在里面有一个滚动条,但是当一个人进行搜索时,内容会发生变化(应用程序的全部目的就是搜索),但仅仅是加载页面就会清空ng视图 有没有停止默认行为的方法?这里有一个选项,用包装默认内容,然后通过默认路径将该模板加载到ng视图中 html <div ng-app="myApp"> <div ng-view> <script i

好的,我需要能够阻止
ng-view
擦除内容,我需要有ng-view,因为它是唯一一个不会(也不能)破坏整个应用程序的地方

我在里面有一个滚动条,但是当一个人进行搜索时,内容会发生变化(应用程序的全部目的就是搜索),但仅仅是加载页面就会清空ng视图


有没有停止默认行为的方法?

这里有一个选项,用
包装默认内容,然后通过默认路径将该模板加载到
ng视图中

html

<div ng-app="myApp">
    <div ng-view>
        <script id="bar.html" type="text/ng-template">
           <ul>
             <li> test1 </li>
             <li> test2 </li>
             <li> test3 </li>
             <li> test4 </li>
            </ul>
        </script>
    </div>
</div>
可能有更好的解决方案,但我还需要做更多的工作。

另一种(也许更好的*)方法是使用指令获取
ng视图的内容,并在编译(和清除)之前将其存储为模板

这样,您就不需要将
ng视图的内容包装到脚本标记中

<div ng-app="myApp">    
    <div foo ng-view>
        <ul>
            <li> test1 </li>
            <li> test2 </li>
            <li> test3 </li>
            <li> test4 </li>
        </ul>
    </div>
</div>

  • 测试1
  • 测试2
  • 测试3
  • 测试4


*)我自己对angular是个新手,所以我对angular的心态还不够深刻,不知道做这些事情是否完全“合适”。

结合@towr和


不要忘记将优先级设置为400以上,因为ng视图是以优先级400执行的

例如:

.directive('foo', ['$templateCache', function($templateCache){
return {
    restrict: 'A', 
    priority:500,
    compile:  function (element)
    {
        $templateCache.put('bar.html', element.html());
    }
};}])

视图是呈现内容的地方,因此,如果您有默认内容,通常的方法是使用模板将其呈现在那里。有关类似问题,请参阅
<div ng-app="myApp">    
    <div foo ng-view>
        <ul>
            <li> test1 </li>
            <li> test2 </li>
            <li> test3 </li>
            <li> test4 </li>
        </ul>
    </div>
</div>
angular.module('myApp', ['ngRoute'])
.directive('viewWithContent', ['$templateCache', function($templateCache)
{
    return {
        restrict: 'A',
        compile:  function (element)
        {
            $templateCache.put('initial-view.html', element.html());
        }
    };
}])
.config(function($routeProvider, $locationProvider) 
{
    var initialized = false;
    $routeProvider.when('/', {
        templateUrl: function(){
            if(initialized){ 
                return './views/route-url.html';
            }

            initialized = true;

            return 'initial-view.html';
        },
        controller: 'someController'
    });
});
.directive('foo', ['$templateCache', function($templateCache){
return {
    restrict: 'A', 
    priority:500,
    compile:  function (element)
    {
        $templateCache.put('bar.html', element.html());
    }
};}])