【问题标题】:Angular ng-view is not replacing the viewAngular ng-view 不会替换视图
【发布时间】:2015-12-19 04:08:42
【问题描述】:

我有以下代码

<!DOCTYPE html>
<html ng-app="mainApp">
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-route.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-resource.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="../modules/mainApp.js"></script>
</head>
    <body>
        <h1>{{Title}}</h1>
        <div ng-view>
        </div>
    </body>
</html>

我的angularjs文件如下

var mainApp = angular.module('mainApp',['ngRoute']).run(function($rootScope){
    $rootScope.Title = "Alpha Bravo Charlie";
});

//routing
mainApp.config(function($routeProvider){
    alert('hello');
     alert("hello2");
    $routeProvider
    .when('/',
    {
        templateUrl:'views/view1.html',
        controller: 'CustController'

    });
});

mainApp.controller('CustController',function($scope){
    $scope.customers=[
            {name:'Dave Jones', city:'Dubai', number: '00001'},
            {name:'Nouman Ali', city:'Lahore', number: '00002'},
            {name:'Winz Cruz', city:'Dubai', number: '00003'},
            {name:'Roberto Mancini', city:'Per', number: '00004'},
            {name:'Dave Ands', city:'Abu Dhabi', number: '00005'},
        ];
});

它相应地更改了标题,但我实际上是在运行时替换视图,但是当我加载 index.html 页面时没有任何反应,并且控制台上发生错误

错误:无法在“历史”上执行“replaceState”:历史状态 带有 URL 的对象 'file:///D:/practice_code/nodejs/angular/views/index.html#/D:/' 不能 在原点为“null”的文档中创建。

【问题讨论】:

  • 该错误看起来可能与file:// 协议的问题有关。当它从本地安装的 Web 服务器运行时,您可以复制它吗?
  • 你需要运行webserver,不要使用文件协议。
  • 是的,在运行网络服务器后,404。但是当我使用像“views/view1.html”这样的路径时,它会附加一个额外的“views”并且路径变得像“views/views/view1.html”但是如果我直接使用“view1.html”然后路径变为“localhost:11012/view1.html404(未找到)”

标签: jquery angularjs angularjs-directive angularjs-scope angularjs-routing


【解决方案1】:

请在此处检查或发布您的项目文件夹结构以及您运行网络服务器的位置。根据我的观察,“D:/practice_code/nodejs/angular/views/index.html”的项目路径似乎在views文件夹内,因为index.html在views文件夹下。

请检查并运行您的网络服务器以获取正确的目录

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 2023-03-04
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多