Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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 有没有办法消除Angular Js中的此错误?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 有没有办法消除Angular Js中的此错误?

Javascript 有没有办法消除Angular Js中的此错误?,javascript,html,angularjs,Javascript,Html,Angularjs,我是Angular JS的新手,我尝试在其中使用路由,但我面临一些问题。 主要 <a href="#!london">City 1</a> <a href="#!paris">City 2</a> <p>Click on the links.</p> <p> Note that each "view" has its own control

我是Angular JS的新手,我尝试在其中使用路由,但我面临一些问题。


主要

<a href="#!london">City 1</a>
<a href="#!paris">City 2</a>

<p>Click on the links.</p>

<p>
  Note that each "view" has its own controller which each gives the "msg"
  variable a value.
</p>

<div ng-view></div>

<script>
  var app = angular.module("myApp", ["ngRoute"]);
  app.config(function ($routeProvider) {
    $routeProvider
      .when("/", {
        templateUrl: "main.htm",
      })
      .when("/london", {
        templateUrl: "london.htm",
        controller: "londonCtrl",
      })
      .when("/paris", {
        templateUrl: "paris.htm",
        controller: "parisCtrl",
      });
  });
  app.controller("londonCtrl", function ($scope) {
    $scope.msg = "I love London";
  });
  app.controller("parisCtrl", function ($scope) {
    $scope.msg = "I love Paris";
  });
</script>

点击链接

请注意,每个“视图”都有自己的控制器,每个控制器都提供“msg” 变量是一个值。

var-app=angular.module(“myApp”[“ngRoute”]); app.config(函数($routeProvider){ $routeProvider .当(“/”时{ templateUrl:“main.htm”, }) .when(“/伦敦”{ templateUrl:“london.htm”, 控制员:“伦敦控制中心”, }) .when(“/paris”{ templateUrl:“paris.htm”, 控制器:“parisCtrl”, }); }); 应用控制器(“londonCtrl”,功能($scope){ $scope.msg=“我爱伦敦”; }); 应用控制器(“parisCtrl”,功能($scope){ $scope.msg=“我爱巴黎”; });
london.htm

<h1>London</h1>
<h1>Paris</h1>
伦敦
伦敦是英国的首都。 它是英国人口最多的城市,拥有大都市区 超过1300万居民。

{{msg}}

paris.htm

<h1>London</h1>
<h1>Paris</h1>
巴黎
巴黎是法国的首都。 巴黎地区是欧洲最大的人口中心之一,拥有1200多万居民


{{msg}

发生这些错误是因为您没有在任何服务器或本地主机下运行它。如果直接在浏览器下打开文件,Angular JS路由将不起作用

如何解决这个问题 安装wamp服务器或任何其他服务器。将文件移动到服务器根目录,并使用其地址运行它。

如果使用WAMP服务器,请将文件复制到文件夹中,并将文件夹移动到C:/Wamp64/www/
在web浏览器中,键入:http://localhost/

为了便于参考,我还添加了代码:
index.html


角样

var-app=angular.module(“myApp”[“ngRoute”]); app.config(函数($routeProvider){ $routeProvider .当(“/”时{ templateUrl:“pages/red.html” }) .when(“/red”{ templateUrl:“pages/red.html” }) .when(“/绿色”{ templateUrl:“pages/green.html” }) .when(“/blue”{ templateUrl:“pages/blue.html” }); });
pages/green.html


身体{
背景颜色:绿色;
颜色:#ffffff;
}
绿色
pages/red.html


身体{
背景色:红色;
颜色:#ffffff;
}
红色
pages/blue.html


身体{
背景颜色:蓝色;
颜色:#ffffff;
}
蓝色

发生这些错误是因为您没有在任何服务器或本地主机下运行它。如果直接在浏览器下打开文件,Angular JS路由将不起作用

如何解决这个问题 安装wamp服务器或任何其他服务器。将文件移动到服务器根目录,并使用其地址运行它。

如果使用WAMP服务器,请将文件复制到文件夹中,并将文件夹移动到C:/Wamp64/www/
在web浏览器中,键入:http://localhost/

为了便于参考,我还添加了代码:
index.html


角样

var-app=angular.module(“myApp”[“ngRoute”]); app.config(函数($routeProvider){ $routeProvider .当(“/”时{ templateUrl:“pages/red.html” }) .when(“/red”{ templateUrl:“pages/red.html” }) .when(“/绿色”{ templateUrl:“pages/green.html” }) .when(“/blue”{ templateUrl:“pages/blue.html” }); });
pages/green.html


身体{
背景颜色:绿色;
颜色:#ffffff;
}
绿色
pages/red.html


身体{
背景色:红色;
颜色:#ffffff;
}
红色
pages/blue.html


身体{
背景颜色:蓝色;
颜色:#ffffff;
}
蓝色

是,除非其他服务器支持跨源请求,否则不要进行跨源请求-但是,这种情况下的错误更简单。。。您需要使用http服务器来服务您的页面<代码>文件://不是用于进行任何类型开发的协议。。。至少在本地服务器上使用
http
。我没有向服务器请求任何文件。我很困惑。不,你不是在
http
服务器上这么做的。。。您正在使用
文件://
协议加载页面-您发布的错误与加载页面的方式无关,这意味着我必须部署angular js应用程序。您可以修改代码吗?是的,除非其他服务器支持跨源请求,否则不要进行跨源请求-但是,这种情况下的错误更简单。。。您需要使用http服务器来服务您的页面<代码>文件://不是用于进行任何类型开发的协议。。。至少在本地服务器上使用
http
。我没有向服务器请求任何文件。我很困惑。不,你不是d
<html>
    <head>
        <style>
            body{
                background-color: green;
                color:#ffffff;
            }
        </style>
        <body>
            <h1>Green</h1>
        </body>
    </head>
</html>
<html>
    <head>
        <style>
            body{
                background-color: red;
                color:#ffffff;
            }
        </style>
        <body>
            <h1>Red</h1>
        </body>
    </head>
</html>
<html>
    <head>
        <style>
            body{
                background-color: blue;
                color:#ffffff;
            }
        </style>
        <body>
            <h1>Blue</h1>
        </body>
    </head>
</html>