81

I wanted to load an inline view template.

I wrapped the template in a script tag of type text/ng-template and set the id to temp1.html. and here's what my module config looks like

learningApp.config(function ($routeProvider) {
    $routeProvider
        .when("/first",{ controller: "SimpleController", templateUrl: "temp1.html"})
        .when("/second", {controller: "SimpleController", templateUrl: "temp2.html"})
        .otherwise({redirectTo : "/first"});
});

It tells me GET http://localhost:41685/temp1.html 404 (Not Found) in my console window meaning that it's looking for a file of that name.

My Question is: How do I configure my routes to use inline templates?

Update: Here's what my server-rendered DOM looks like

<!DOCTYPE html>
<html>
<head>
    <script src="/Scripts/angular.js"></script>
    <link href="/Content/bootstrap.css" rel="stylesheet"/>
</head>
<body>
    <div class="container">       
    <h2>Getting Started with Angular</h2>
    <div class="row">
        <div class="panel" ng-app="LearningApp">
            <div ng-view></div>
        </div>
    </div>

<script type="text/ng-template" id="temp1.html">
    <div class="view">
        <h2>First View</h2>
        <p>
            Search:<input type="text" ng-model="filterText" />
        </p>
        <ul class="nav nav-pills">
            <li ng-repeat="cust in customers | orderBy:'name' | filter: filterText "><a href="#">{{cust.name}} - {{cust.school}}</a></li>
        </ul>
    </div>
</script>

<script type="text/ng-template" id="temp2.html">
    <div class="view">
        <h2>Second View</h2>
        <p>
           Search:<input type="text" ng-model="filterText" />
        </p>
        <ul class="nav nav-pills">
            <li ng-repeat="cust in customers | orderBy:'name' | filter: filterText "><a href= "#">{{cust.name}} - {{cust.school}}</a></li>
        </ul>
    </div>
</script>
    </div>
    <script src="/Scripts/jquery-1.9.1.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
    <script src="/Scripts/app/LearningApp.js"></script>
 </body>
</html>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Ody
  • 2,012
  • 4
  • 25
  • 35

2 Answers2

110

Ody, you were on the right track, the only problem was that the tags are outside of the DOM element on which the ng-app directive is used. If you move it to the <body ng-app="LearningApp"> element in-line templates should work.

You might also find this question relevant: Is there a way to make AngularJS load partials in the beginning and not at when needed?

Community
  • 1
  • 1
pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286
  • 2
    I now see why most tutorials prefer to add the ng-app directive to the html element. That's because anything that is related to that app/module must be within the ng-app scope hence putting it in the html tag avoids future problems like this – Ody Apr 21 '13 at 09:20
  • 3
    This is something the documentation has to mention. I lost hours wondering why those 404 appeared... – Rob Juurlink Dec 02 '13 at 22:20
33

Try use the id-Attribute of the script-Element to set the name of the template should work.

<script type="text/ng-template" id="temp1.html">
   ... some template stuff
</script>
tschiela
  • 5,231
  • 4
  • 28
  • 35
  • I did that. Didn't work. I'm suspecting the fact that my main page is actually not typical html file. as in http://server/controller/ – Ody Apr 20 '13 at 20:37
  • why you dont use template files, its more clear in my opionion. – tschiela Apr 20 '13 at 20:44
  • Yeah you're right. That method works well, performs better and all. But I have applications that I want to migrate that were written in older frameworks like backbone and they rely heavily on inline templates. That's why I wanted to know how it's done in angular. – Ody Apr 20 '13 at 20:50