简单的ng - 包括不工作

Oam*_*Psy 68 html javascript angularjs angularjs-scope

我是第一次玩AngularJS,我努力使用ng-include作为我的页眉和页脚.

这是我的树:

myApp
assets
   - CSS
   - js
        - controllers
        - vendor
             - angular.js
             - route.js
             ......
             ......
             ......
        main.js
pages
   - partials
        - structure
              header.html
              navigation.html
              footer.html
   index.html
   home.html
Run Code Online (Sandbox Code Playgroud)

index.html的:

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>AngularJS Test</title>

    <script src="/assets/js/vendor/angular.js"></script>
    <script src="/assets/js/vendor/route.js"></script>
    <script src="/assets/js/vendor/resource.js"></script>
    <script src="/assets/js/main.js"></script>

</head>
    <body>          

        <div ng-include src="partials/structure/header.url"></div>
        <div ng-include src="partials/structure/navigation.url"></div>    

        <div id="view" ng-view></div>   

        <div ng-include src="partials/structure/footer.url"></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

main.js

var app = angular.module("app", ["ngResource", "ngRoute"]);


app.config(function($routeProvider) {

$routeProvider.when("/login", {
    templateUrl: "login.html",
    controller: "LoginController"
});

$routeProvider.when("/home", {
    templateUrl: "home.html",
    controller: "HomeController"
});

$routeProvider.otherwise({ redirectTo: '/home'});

});

app.controller("HomeController", function($scope) {
    $scope.title = "Home";
});
Run Code Online (Sandbox Code Playgroud)

home.html的

<div>
<p>Welcome to the {{ title }} Page</p>
</div>
Run Code Online (Sandbox Code Playgroud)

当我进入home.html页面时,我的标题,导航和页脚没有出现.

Pie*_*len 145

你正在做一个标题包含.url而不是标题.HTML.看起来你想在src属性中使用文字,所以你应该用引号将它们包装起来,正如@DRiFTy在评论中提到的那样.

更改

 <div ng-include src="partials/structure/header.url"></div>
 <div ng-include src="partials/structure/navigation.url"></div>    

 <div id="view" ng-view></div>   

 <div ng-include src="partials/structure/footer.url"></div>
Run Code Online (Sandbox Code Playgroud)

 <div ng-include src="'partials/structure/header.html'"></div>
 <div ng-include src="'partials/structure/navigation.html'"></div>    

 <div id="view" ng-view></div>   

 <div ng-include src="'partials/structure/footer.html'"></div>
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请检查浏览器控制台是否有404

  • 要添加到这个,你需要用单引号包装路径:`src ="'partials/structure/header.html'"` (83认同)
  • @DiscGolfer这是非常重要的信息.在这个工作之前我检查了我的代码很久了!+1 (3认同)
  • 它更加灵活.我用它来嵌入函数调用,它可以动态生成相应的url. (2认同)

小智 98

我有一个类似的问题,简单的ng-include不渲染:

<div ng-include="views/footer.html"></div>

我用单引号包装文件路径,它现在呈现:

<div ng-include="'views/footer.html'"></div>

  • 你节省了我的时间!谢谢 (7认同)
  • 你需要用单引号包装字符串文字(例如你的文件路径),因为完整引号中的任何内容都将被评估为代码,因此如果没有单引号,Angular编译器就会在某个地方使用文件路径的名称. . (2认同)

Gra*_*amF 18

我有一个类似的问题让我来到这里.

ng-include 没有填充我的部分内容,但没有控制台错误,也没有404的.

然后我意识到我做了什么.

为我解决:确保你有ng-app外面的ng-include!如此明显 作为Angular noob的价格.

  • JEEZ,这是难以追查的信息.我知道这听起来很荒谬,但**这么多的**文章都把这部分留了下来.即便是一对"Hello World"教程也没有提到它.我找到了这个答案然后找到一个工作的JSfiddle来学习我需要`<div ng-app>`.荒诞. (2认同)