AngularJS:错误:$ compile:tpload错误在加载页面时加载模板?

时间:2017-07-13 12:03:48

标签: angularjs angular-ui-router angularjs-routing

enter image description here

我必须在Angularjs中执行路由功能。我在加载页面时遇到错误,并单击选项卡。如何修复此错误?



<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>

<body ng-app="myApp">
  <p><a href="#/!">Main</a></p>
  <a href="#!red">Red</a>
  <a href="#!green">Green</a>
  <a href="#!blue">Blue</a>

  <div ng-view></div>
    <script>
        var app = angular.module("myApp", ["ngRoute"]);
        app.config(function($routeProvider) {
            $routeProvider
            .when("/", {
                templateUrl : "main.html"
            })
            .when("/red", {
                templateUrl : "red.html"
            })
            .when("/green", {
                templateUrl : "green.html"
            })
            .when("/blue", {
                templateUrl : "blue.html"
            });
        });
    </script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您必须在索引文件所在的目录中包含相应的HTML文件main.htmlred.htmlgreen.htmlblue.html

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "main.html"
    })
    .when("/red", {
        templateUrl : "red.html"
    })
    .when("/green", {
        templateUrl : "green.html"
    })
    .when("/blue", {
        templateUrl : "blue.html"
    });
});

Working Plunker https://plnkr.co/edit/NqvLYxo66Yj2U7Ytw1DX?p=preview